An important web server task is serving files (such as images or static HTML pages).
Depending on the request, files will be served from different local directories: /data/www (which may contain HTML files) and /data/images (which may contain images). This will require editing the configuration file and setting up the server block inside the http block using two location blocks. (Recommended learning:nginx use)
First, create the /data/www directory and put an index.html file containing any text content into it, and create / data/images directory and put some images in it.Create two directories-
[root@localhost ~]# mkdir -p /data/www [root@localhost ~]# mkdir -p /data/images [root@localhost ~]#
Put two files in the two directories created above: /data/www/index.html and /data/images/logo.png, /data The content of the /www/index.html file is one line, as follows -
New Static WebSite Demo.
Next, open the configuration file (/usr/local/nginx/conf/nginx.conf). The default configuration file already contains several examples of server blocks, most of which are commented out. Now comment out all such blocks and start a new server block:
http { server { } }
Typically, the configuration file can include several server blocks distinguished by the port the server listens on and the server name. When nginx decides which server to handle the request, it tests the URI specified in the request header against the parameters of the location directive defined inside the server block.
Add the following location block to the server block:
http { server { location / { root /data/www; } } }
http { server { location / { root /data/www; } location /images/ { root /data; } } }
The final configuration of the server block should look like this:
server { location / { root /data/www; } location /images/ { root /data; } }
This is already a server listening on the standard port 80 and accessible on the local machine ( http://localhost/ ) working configuration. In response to a request for a URI starting with /images/, the server will send files from the /data/images directory. For example, in response to a http://localhost/images/logo.png request, nginx will send the /data/images/logo.png file on the service. If the file does not exist, nginx will send a response indicating a 404 error. Requests for URIs not starting with /images/ will be mapped to the /data/www directory. For example, in response to a request for http://localhost/about/example.html, nginx will send the /data/www/about/example.html file.
To apply the new configuration, start nginx if it has not been done yet or send a reload signal to nginx's main process by executing the following command:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
Open a browser or use CURL to access the Nginx server as shown below-
The complete nginx.conf file configuration content is as follows:
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; ## 新服务(静态网站) server { location / { root /data/www; } location /images/ { root /data; } } }
The above is the detailed content of Serving static content using nginx. For more information, please follow other related articles on the PHP Chinese website!