Serving static content using nginx

(*-*)浩
Release: 2019-11-30 14:37:38
Original
3347 people have browsed it

Serving static content using nginx

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 ~]#
Copy after login

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.

Copy after login

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 { } }
Copy after login

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; } } }
Copy after login
The location block specifies the "/" prefix that is compared to the URI in the request . For matching requests, the URI will be added to the path specified in the root directive (i.e. /data/www) to form the path to the requested file on the local file system. If there are several matching location blocks, nginx will choose the one with the longest prefix to match the location block. The location block above provides the shortest prefix length of 1, so this block will only be used if all other location blocks cannot provide a match.

Next, add a second location block:

http { server { location / { root /data/www; } location /images/ { root /data; } } }
Copy after login
It will also match requests starting with /images/(location/, but with a shorter prefix , that is, "/images/" is longer than "/") to match the request.

The final configuration of the server block should look like this:

server { location / { root /data/www; } location /images/ { root /data; } }
Copy after login

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
Copy after login
If an error or exception prevents the If it works normally, you can try to check the access.log and error.log files in the directory /usr/local/nginx/logs or /var/log/nginx to find the reason.

Open a browser or use CURL to access the Nginx server as shown below-

Serving static content using nginx

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; } } }
Copy after login

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!

Related labels:
source:php.cn
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
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!