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; } }
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; }
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#
Restart nginx
root@ubuntu:/usr/local/nginx/sbin# ./nginx -s reload root@ubuntu:/usr/local/nginx/sbin#
Open the browser and enter server_name:80/image/1.jpg to access the static image as shown below
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!