How to configure nginx static resource server

WBOY
Release: 2023-05-19 10:13:05
forward
2200 people have browsed it

Enter the conf directory of the nginx installation directory, modify the nginx.conf file, and add a location in a server{}. Part of the configuration code is as follows

root@ubuntu:/usr/local/nginx/conf# vi nginx.conf
 server {
    listen    80;
    server_name localhost;
    location / {
      root  html;
      index index.html index.htm;
    }
    location /image/ {
      root  /usr/local/myimage/;
      autoindex on;
    }

  }
Copy after login

From the above configuration, it can be seen that the port is 80, server_name For localhost (you can also write the IP address)

location /image/ {
      root  /usr/local/myimage/;
      autoindex on;
    }
Copy after login

This configuration means that when you enter localhost:80/image/, the /usr/local/myimage/image/ directory of the local machine will be accessed. Therefore, we need to create a new /usr/local/myimage/image/ directory, and at the same time create a new image directory with the same name as the image in the location in the html directory of the nginx installation directory. Although there is nothing in the directory, in /usr/local/ We put a picture 1.jpg in my image/image/, restart the nginx service, and you can access it through localhost:80/image/1.jpg

root@ubuntu:/usr/local/nginx/html# mkdir image

root@ubuntu:/usr/local/nginx/html# mkdir /usr/local/myimage/image
#放一张照片上去#
root@ubuntu:/usr/local/nginx/html# cd /usr/local/myimage/image
root@ubuntu:/usr/local/myimage/image# ls
1.jpg
root@ubuntu:/usr/local/myimage/image#
Copy after login

Restart nginx

root@ubuntu:/usr/local/nginx/sbin# ./nginx -s reload
root@ubuntu:/usr/local/nginx/sbin#
Copy after login

Open the browser and enter server_name:80/image/1.jpg to access the static image as shown below

How to configure nginx static resource server

The above is the detailed content of How to configure nginx static resource server. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!