1. 程式人生 > >nginx反向代理,實現位址列域名不變,session不失效

nginx反向代理,實現位址列域名不變,session不失效

首先找到nginx.conf部分

 server {
        listen 80;
        server_name www.taekwondo-china.com;
        location / { #所有以/開頭的地址,實際上就是所有請求
          proxy_pass http://event.geexek.com/7918/;
        }
        location ^~ /7918 {
            rewrite ^/(.*) /$1 break;
            proxy_pass http://event.geexek.com;
        }

        location
~ .*\.(js|css|gif|jpg|png|ico|jpeg)$ { rewrite ^/(.*) /$1 break; proxy_pass http://event.geexek.com; } location ^~ /errorpage { root /home/nginx/nginx_data; } error_page 500 502 503 504 /errorpage/500.html; error_page 400 404 /errorpage/404.html; }
  location / { #所有以/開頭的地址,實際上就是所有請求
          proxy_pass http://event.geexek.com/7918/;
        }

在瀏覽器的位址列裡面輸入:www.taekwondo-china.com,將請求轉發到http://event.geexek.com/7918/,得到響應返回www.taekwondo-china.com,顯示來自http://event.geexek.com/7918/,但是位址列裡面的地址還是www.taekwondo-china.com。

 location ^~ /7918 {
            rewrite
^/(.*) /$1 break; proxy_pass http://event.geexek.com; }

location ^~ /7918

rewrite ^/(.*) /$1 break;

rewrite後面的引數是一個簡單的正則 ^/(.*) /::匹配字串的結束
(.).代表任何字元 代表所有的,也就是(.*)選取了/7918/之後的所有字元並存入變數1.1是重寫後的表示式。

最後的引數共有四個取值:
last 處理完請求後,跳出location,再重新執行。
break 處理完成之後,不會跳出location,不再重新執行。而只能將資訊傳遞給後面的fsatcgi_pass或者proxy_pass等指令。
permanent 永久性重定向,請求日誌中的狀態碼為301
redirect 臨時重定向,請求日誌中的狀態碼為302

BUT 如果你的系統使用了session,你會發現自己登入不上了。

所以要改一種配置方式

首先要讓session生效

#skip.geexek.cn
    upstream  SKIP  {
        server    10.XX.XXX.181:9704;
        }

    server {
        listen       80;
        server_name  skip.geexek.cn;

        location / {
            proxy_next_upstream error timeout http_500 http_502 http_504;
            proxy_read_timeout 60s;
            proxy_set_header   Host             $host;
            proxy_pass         http://SKIP;
            proxy_set_header   Cookie $http_cookie;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_intercept_errors on;
            client_max_body_size 15M;
            index  index.html index.htm;
            rewrite ^/$  /7235 last;
            }
        location ^~ /errorpage {
        root /home/nginx/nginx_data;
        }

        error_page  500 502 503 504 /errorpage/500.html;
        error_page  400 404 /errorpage/404.html;
        }

我們給10.XX.XXX.181:9704的這個工程反向代理為需要位址列中需要顯示的地址
skip.geexek.cn,配置好之後你使用http://skip.geexek.cn/7235 可以完成使用者的登入,但是 http://skip.geexek.cn 無法找到自己的對應的網站。

rewrite ^/$  http://skip.geexek.cn/7235 break;

url重寫是指通過配置conf檔案,以讓網站的url中達到某種狀態時則定向/跳轉到某個規則,比如常見的偽靜態、301重定向、瀏覽器定向等

rewrite

語法:

在配置檔案的server塊中寫,如:

server {
    rewrite 規則 定向路徑 重寫型別;
}

規則:

可以是字串或者正則來表示想匹配的目標url

定向路徑:

表示匹配到規則後要定向的路徑,如果規則裡有正則,則可以使用$index來表示正則裡的捕獲分組

重寫型別:
last :相當於Apache裡的(L)標記,表示完成rewrite,瀏覽器位址列URL地址不變

break;本條規則匹配完成後,終止匹配,不再匹配後面的規則,瀏覽器位址列URL地址不變

redirect:返回302臨時重定向,瀏覽器地址會顯示跳轉後的URL地址

permanent:返回301永久重定向,瀏覽器位址列會顯示跳轉後的URL地址
簡單例子

server {
    # 訪問 /last.html 的時候,頁面內容重寫到 /index.html 中
    rewrite /last.html /index.html last;
    # 訪問 /break.html 的時候,頁面內容重寫到 /index.html 中,並停止後續的匹配
    rewrite /break.html /index.html break;
    # 訪問 /redirect.html 的時候,頁面直接302定向到 /index.html中
    rewrite /redirect.html /index.html redirect;
    # 訪問 /permanent.html 的時候,頁面直接301定向到 /index.html中
    rewrite /permanent.html /index.html permanent;
    # 把 /html/*.html => /post/*.html ,301定向
    rewrite ^/html/(.+?).html$ /post/$1.html permanent;
    # 把 /search/key => /search.html?keyword=key
    rewrite ^/search\/([^\/]+?)(\/|$) /search.html?keyword=$1 permanent;
}

last和break的區別

因為301和302不能簡單的只返回狀態碼,還必須有重定向的URL,這就是return指令無法返回301,302的原因了。這裡 last 和 break 區別有點難以理解:

last一般寫在server和if中,而break一般使用在location中

last不終止重寫後的url匹配,即新的url會再從server走一遍匹配流程,而break終止重寫後的匹配

break和last都能組織繼續執行後面的rewrite指令

在location裡一旦返回break則直接生效並停止後續的匹配location

server {
    location / {
        rewrite /last/ /q.html last;
        rewrite /break/ /q.html break;
    }
    location = /q.html {
        return 400;
    }
}

訪問/last/時重寫到/q.html,然後使用新的uri再匹配,正好匹配到locatoin = /q.html然後返回了400

訪問/break時重寫到/q.html,由於返回了break,則直接停止了