Nginx 配置詳解

Nginx 安裝完以後位於 /usr/local/nginx 路徑下,配置文件爲 /usr/local/nginx/conf/nginx.conf。php

如下是默認配置html

#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 {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

 

Nginx 代理配置nginx

使用 upstream 增長一組處理請求的 serverweb

...

upstream myserver { 
    server 192.168.10.1:8080;
    server 192.168.10.2:8080;
}

...

 server {
    ...
    location / {       
        proxy_pass  http://myserver;  #請求轉向myserver 定義的服務器列表
    }
    ... 
}

...

這樣全部的請求都會按必定的策略被分配到 myserver 這個組裏面的ip地址上。正則表達式

Nginx 有3種輪詢策略:輪詢,加權輪詢,ip_hash。服務器

輪詢:nginx默認就是輪詢其權重都默認爲1,服務器處理請求的順序:ABABABABAB....cookie

upstream myserver { 
    server 192.168.10.1:8080;
    server 192.168.10.2:8080;
}

加權輪詢:跟據配置的權重的大小而分發給不一樣服務器不一樣數量的請求。下面服務器的請求順序爲:ABBABBABBABBABB....session

upstream myserver { 
    server 192.168.10.1:8080 weight=1;
    server 192.168.10.2:8080 weight=2;
}

ip_hash:nginx會讓相同的客戶端ip請求相同的服務器。app

upstream myserver {
    ip_hash;
    server 192.168.10.1:8080;
    server 192.168.10.2:8080;
}

還有一種是 熱備:若是你有2臺服務器,當一臺服務器發生事故時,才啓用第二臺服務器給提供服務。服務器處理請求的順序:AAAAAA忽然A掛啦,BBBBBBBBBBBBBB.....tcp

upstream myserver { 
    server 192.168.10.1:8080;
    server 192.168.10.2:8080 backup;
}

 

listen 配置

Nginx 配置文件至少包含一個 server 命令 ,用來定義虛擬服務器。當請求到來時, Nginx 會首先選擇一個虛擬服務器來處理該請求。

http {
    ...

    server {
        # Server configuration
    }

    ...
}

server 配置塊使用 listen 命令監聽本機 IP 和端口號

...

server {
    listen       80;
    server_name  localhost;
    ...
}

server {
    listen 8080;
    server_name somename:8080;
    ...
}

server {
    listen 443 ssl;
    server_name localhost;
    ...
}

...

 

location 配置

nginx 會根據 location 配置來決定應該如何處理接收到的請求。

...

location /images/ {
    root /data/images;
}

location / {
    proxy_pass http://192.168.10.1;
}

...

全部 /images/ 開頭的請求都會直接訪問服務器的文件, 其他的請求都被代理到 192.168.10.1 這個服務器上面。如請求爲 /images/1.jpg 被代理後返回 /data/images/1.jpg 這個文件。

location 支持正則表達式,而且匹配規則有必定的優先級。

location 表達式

~   表示執行一個正則匹配,區分大小寫

~*  表示執行一個正則匹配,不區分大小寫

^~  表示普通字符匹配。使用前綴匹配。若是匹配成功,則再也不匹配其餘location。

=   進行普通字符精確匹配。也就是徹底匹配。

@  它定義一個命名的 location,使用在內部定向時,例如 error_page, try_files

location 優先級

1.等號類型(=)的優先級最高。一旦匹配成功,則再也不查找其餘匹配項。

2.^~類型表達式。一旦匹配成功,則再也不查找其餘匹配項。

3.正則表達式類型(~ ~*)的優先級次之。若是有多個location的正則能匹配的話,則使用正則表達式最長的那個。

4.常規字符串匹配類型。按前綴匹配。

(location =) > (location 完整路徑) > (location ^~ 路徑) > (location ~,~* 正則順序) > (location 部分起始路徑) > (/)

 

#以/開頭的請求,優先級最低
location / {
  [A]
}

#精確匹配 / 的請求
location = / {
  [B]
}

#以/images/開頭的請求
location /images/ {
  [C]
}

#以/images/Abc 開頭的請求
location ~ /images/Abc {
  [D]
}

#以 /images/ 開頭的請求, 匹配成功以後則不繼續往下匹配
location ^~ /images/ {
  [E]
}

#匹配以 .jpg .png 結尾的請求,不區分大小寫
location ~* \.(jpg|png)$ {
  [F]
}
#以/images/abc 開頭的請求
location ~ /images/abc {
  [H]
}

/ -> 規則 B

徹底匹配

/images/1.gif -> 規則 E

^~ 前綴匹配 若是匹配成功則中止往下搜索

/images/abc/def -> 規則 E

先匹配到 H 繼續往下查找,匹配到 E 中止往下搜索

 

針對請求參數代理到不一樣服務器

http {
  upstream server1 {
    server 192.168.10.1:8080;
  }
  
  upstream server2 {
    server 192.168.10.2:8081;
  }
    
  ...
  
  server {
    ...
    set $backend_server "server1";
    if ( $request_uri ~ "^arg=baz$" ) {
      set $backend_server  "server2";
    }
    
    location /index.php {
      proxy_pass http://$backend_server;
    }  
  }
}

使用 if 命令 判斷 /index.php 請求的參數 arg 是否是等於 baz,若是是 請求被代理到 192.168.10.2:8081,若是不是 請求被代理到 192.168.10.1:8080。如 /index.php?arg=aa  被代理到 192.168.10.1:8080,/index.php?arg=baz  被代理到 192.168.10.2:8080。

內置變量

$args :這個變量等於請求行中的參數,同$query_string

$content_length :請求頭中的Content-length字段。

$content_type :請求頭中的Content-Type字段。

$document_root : 當前請求在root指令中指定的值。

$host : 請求主機頭字段,不然爲服務器名稱。

$http_user_agent : 客戶端agent信息

$http_cookie : 客戶端cookie信息

$limit_rate : 這個變量能夠限制鏈接速率。

$request_method : 客戶端請求的動做,一般爲GET或POST。

$remote_addr : 客戶端的IP地址。

$remote_port : 客戶端的端口。

$remote_user : 已經通過Auth Basic Module驗證的用戶名。

$request_filename : 當前請求的文件路徑,由root或alias指令與URI請求生成。

$scheme : HTTP方法(如http,https)。

$server_protocol : 請求使用的協議,一般是HTTP/1.0或HTTP/1.1。

$server_addr : 服務器地址,在完成一次系統調用後能夠肯定這個值。

$server_name : 服務器名稱。

$server_port : 請求到達服務器的端口號。

$request_uri : 包含請求參數的原始URI,不包含主機名,如:」/foo/bar.php?arg=baz」。

$uri : 不帶請求參數的當前URI,$uri不包含主機名,如」/foo/bar.html」。

$document_uri : 與$uri相同。

 

proxy_pass url 後面加不加 / 的問題

在nginx中配置proxy_pass時,當在後面的url加上了/,至關因而絕對根路徑,則nginx不會把location中匹配的路徑部分代理走;若是沒有/,則會把匹配的路徑部分也給代理走。

用http://192.168.10.3/proxy/test.html 進行訪問。

第一種:

location /proxy/ {
     proxy_pass http://127.0.0.1:8081/;
}

會被代理到http://127.0.0.1:8081/test.html 這個url

第二種:

location /proxy/ {
     proxy_pass http://127.0.0.1:8081;
}

會被代理到http://127.0.0.1:8081/proxy/test.html 這個url

第三種:

location /proxy/ {
     proxy_pass http://127.0.0.1:8081/ftlynx/;
}

會被代理到http://127.0.0.1:8081/ftlynx/test.html 這個url
 
第四種:

location /proxy/ {
     proxy_pass http://127.0.0.1:8081/ftlynx;
}

會被代理到http://127.0.0.1:8081/ftlynxtest.html 這個url

 

參考連接

http://www.cnblogs.com/knowledgesea/p/5199046.html

https://lufficc.com/blog/configure-nginx-as-a-web-server

http://blog.csdn.net/xy2204/article/details/47749405

http://www.jb51.net/article/78746.htm