Nginx-config.conf

1.
nginx has one master process and several worker processes. The main purpose of the master process is to read and evaluate configuration, and maintain worker processes.

2.
By default, the configuration file is named nginx.conf and placed in the directory /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx.

3.
When Nginx is started, you can use -s parameter to control the Nginx:

  1. nginx -s signal

Where signal may be one of the following:

stop — fast shutdown
quit — graceful shutdown
reload — reloading the configuration file
reopen — reopening the log files

4.
You can also do following to send signal.
First, you should know the pid file is stored nginx.pid in the directory /usr/local/nginx/logs or /var/run. you can do:

  1. kill -s QUIT 1628

Of course, you can access http://nginx.org/en/docs/control.html to get more detailed information.

5.
Serving Static Content.
The location block.

  1. location / {
  2.     root /data/www;
  3. }

This location block specifies the “/” prefix compared with the URI from the request. For matching requests, the URI will be added to the path specified in the root directive, that is, to /data/www, to form the path to the requested file on the local file system. If there are several matching location blocks nginx selects the one with the longest prefix. The location block above provides the shortest prefix, of length one, and so only if all other location blocks fail to provide a match, this block will be used.
如果请求的路径是“/”,那资源将会从本地系统(server)root=/data/www里面读取资源,如果有多个location blocks,nginx将会选族prefix最长的那个,上面的block时prefix最短的一个,仅当其他blocks都没有被匹配时,才会调用上面这个最短的.

6.
Next example.

  1. location /images/ {
    
  2.     root /data;
    
  3. }

This will match the request started with /images/, of course, the above one will match but it has shorter prefix so it will not be used.

so the total location is :

server {
    location / {
        root /data/www;
    }

    location /images/ {
        root /data;
    }
}

In response to requests with URIs starting with /images/, the server will send files from the /data/images directory.
For example, in response to the http://localhost/images/example.png request nginx will send the /data/images/example.png file.
If such file does not exist, nginx will send a response indicating the 404 error. Requests with URIs not starting with /images/ will be mapped onto the /data/www directory. For example, in response to the http://localhost/some/example.html request nginx will send the /data/www/some/example.html file.
如果请求的url时http://localhost/webserver/xxx.html will match the location / so the real path is /data/www/webserver/xxx.html.当对config文件进行更改后,请reload Nginx,

  1. ./nginx -s reload

7. server {
listen 8080;
root /data/up1;

location / {
}
}

如果location里面没有root,则会使用上面的root /data/up1

8.

  1. location ~ \.(gif|jpg|png)$ {
    
  2.     root /data/images;
    
  3. }

~表示后面的时正则表达式.

  1. server {
    
  2.     location / {
    
  3.         proxy_pass http://localhost:8080/;
    
  4.     }
    
  5.  
  6.     location ~ \.(gif|jpg|png)$ {
    
  7.         root /data/images;
    
  8.     }
    
  9. }

The parameter is a regular expression matching all URIs ending with .gif, .jpg, or .png. A regular expression should be preceded with ~. The corresponding requests will be mapped to the /data/images directory.
Remembering location with the longest prefix, and then checks regular expressions.
Nginx正则表达式参考:http://blog.csdn.net/a519640026/article/details/9138487

9.

  1. location  = / {
    
  2.   # 只匹配"/".
    
  3.   [ configuration A ] 
    
  4. }
    
  5. location  / {
    
  6.   # 匹配任何请求,因为所有请求都是以"/"开始
    
  7.   # 但是更长字符匹配或者正则表达式匹配会优先匹配
    
  8.   [ configuration B ] 
    
  9. }
    
  10. location ^~ /images/ {
    
  11.   # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location
    
  12.   [ configuration C ] 
    
  13. }
    
  14. location ~* \.(gif|jpg|jpeg)$ {
    
  15.   # 匹配以 gif, jpg, or jpeg结尾的请求. 
    
  16.   # 但是所有 /images/ 目录的请求将由 [Configuration C]处理.   
    
  17.   [ configuration D ] 
    
  18. }

请求URI例子:

/ -> 符合configuration A
/documents/document.html -> 符合configuration B
/images/1.gif -> 符合configuration C
/documents/1.jpg ->符合 configuration D

http://www.cszhi.com/20120513/nginx_nginx-conf.html

发布者

690130229

coder,喜欢安静,喜欢读书,wechat: leslie-liya

发表评论