1. 程式人生 > >nginx教程第十篇:應用舉例 & 踩過的坑

nginx教程第十篇:應用舉例 & 踩過的坑

一、應用舉例

1、叢集配置

upstream mytomcat{
    #分權 即訪問8081與8082的次數比例為1比1
    server localhost:8081 weight=1;
    server localhost:8082 weight=1;
}

server {
    listen 80;
    server_name test.goku.com;

    location / {
        #使用mytomcat分配規則,即剛自定義新增的upstream節點
       proxy_pass http://mytomcat;
    }
}

2、單節點配置中server的listen的作用

 server {
    listen 8084;
    server_name test.goku.com;

    location / {
        proxy_pass http://localhost:8081;
    }
}

此時訪問http://test.goku.com:8084 就相當於訪問 http://localhost:8081

3、靜態資源的訪問

server {
    listen 8085;
    server_name test.goku.com;

    location /MyImage/ {
        root /home/lq/Downloads;
    }
}

訪問http://test.goku.com:8085/MyImage/1.jpg 就相當於訪問 /home/lq/Downloads/MyImage/下面的1.jpg

二、踩過的坑

1、proxy_pass

配置如下:

 server {
    listen 8084;
    server_name test.goku.com;

    location /myweb/ {
        proxy_pass http://localhost:8081;
    }
}
原因,請仔細看下面proxy_pass的用法:

proxy_pass指令的用法:

介紹

該指令用來設定被代理伺服器的地址,可以是主機名稱、IP地址加埠號等形式,其語法結構為:

proxy_pass URL;

其中,URL為要設定的被代理伺服器的地址,包含傳輸協議、主機名稱、IP地址加埠號、URI等要素。傳輸協議通常是“http”或者“https”,指令同時還接受以“unix”開始的UNIX-domain套接字路徑。例如:

proxy_pass http://www.myweb.name/uri;
proxy_pass http://localhost:8000/uri/;
proxy_pass http://unix:/tmp/backend.socket:/uri/;
細節

在nginx中配置proxy_pass代理轉發時,如果在proxy_pass後面的url加/,表示絕對根路徑;如果沒有/,表示相對路徑,把匹配的路徑部分也給代理走。

假設下面四種情況分別用 http://192.168.1.1/proxy/test.html 進行訪問。

第一種:
location /proxy/ {
    proxy_pass http://127.0.0.1/;
}
代理到URL:http://127.0.0.1/test.html


第二種(相對於第一種,最後少一個 / )
location /proxy/ {
    proxy_pass http://127.0.0.1;
}
代理到URL:http://127.0.0.1/proxy/test.html


第三種:
location /proxy/ {
    proxy_pass http://127.0.0.1/aaa/;
}
代理到URL:http://127.0.0.1/aaa/test.html


第四種(相對於第三種,最後少一個 / )
location /proxy/ {
    proxy_pass http://127.0.0.1/aaa;
}
代理到URL:http://127.0.0.1/aaatest.html
舉例
server {
    listen      80;
    server_name www.test.com;

    # 情形A
    # 訪問 http://www.test.com/testa/aaaa
    # 後端的request_uri為: /testa/aaaa
    location ^~ /testa/ {
        proxy_pass http://127.0.0.1:8801;
    }
    
    # 情形B
    # 訪問 http://www.test.com/testb/bbbb
    # 後端的request_uri為: /bbbb
    location ^~ /testb/ {
        proxy_pass http://127.0.0.1:8801/;
    }

    # 情形C
    # 下面這段location是正確的
    location ~ /testc {
        proxy_pass http://127.0.0.1:8801;
    }

    # 情形D
    # 下面這段location是錯誤的
    #
    # nginx -t 時,會報如下錯誤: 
    #
    # nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular 
    # expression, or inside named location, or inside "if" statement, or inside 
    # "limit_except" block in /opt/app/nginx/conf/vhost/test.conf:17
    # 
    # 當location為正則表示式時,proxy_pass 不能包含URI部分。本例中包含了"/"
    location ~ /testd {
        proxy_pass http://127.0.0.1:8801/;   # 記住,location為正則表示式時,不能這樣寫!!!
    }

    # 情形E
    # 訪問 http://www.test.com/ccc/bbbb
    # 後端的request_uri為: /aaa/ccc/bbbb
    location /ccc/ {
        proxy_pass http://127.0.0.1:8801/aaa$request_uri;
    }

    # 情形F
    # 訪問 http://www.test.com/namea/ddd
    # 後端的request_uri為: /yongfu?namea=ddd
    location /namea/ {
        rewrite    /namea/([^/]+) /yongfu?namea=$1 break;
        proxy_pass http://127.0.0.1:8801;
    }

    # 情形G
    # 訪問 http://www.test.com/nameb/eee
    # 後端的request_uri為: /yongfu?nameb=eee
    location /nameb/ {
        rewrite    /nameb/([^/]+) /yongfu?nameb=$1 break;
        proxy_pass http://127.0.0.1:8801/;
    }

    access_log /data/logs/www/www.test.com.log;
}

server {
    listen      8801;
    server_name www.test.com;
    
    root        /data/www/test;
    index       index.php index.html;

    rewrite ^(.*)$ /test.php?u=$1 last;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }

    access_log /data/logs/www/www.test.com.8801.log;
}