1. 程式人生 > >windows上 nginx 配置代理服務,配置多域名,以及最簡單實現跨域配置

windows上 nginx 配置代理服務,配置多域名,以及最簡單實現跨域配置

Nginx,不用多說啦,大家都熟悉的不能再熟悉了,它是一款輕量級的高效能Web 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,最近在本地研究將nginx和resin配合使用,使服務效能達到最高,在配置過程中主要涉及到單域名配置代理服務,以及配置多域名代理服務,以及最簡單實現跨域配置(當然什麼負載均衡,動靜分離,靜態資源代理這些就不說啦,直接放到程式碼裡去了,有註釋)。

在正式上線前,先在本地window環境下配置跑起來測試下配置是否正確,所以這次就以windows 版的nginx做測試了,正式上線後,配置也就相差無幾了。

一、nginx下載、安裝及啟動
下載地址:nginx


下載最新版的nginx for windows版本,下載完成後,解壓做zip包到本地磁碟上,例如:D:\hwy\nginx-1.8.0
啟動:

1、D:\hwy\nginx-1.8.0\start nginx(推薦)
2、D:\hwy\nginx-1.8.0\nginx.exe

注意:推薦使用第一種方式啟動,因為第二種方式會使你的cmd視窗一直處於執行狀態,沒法輸入其他命令了,而第一種已後臺的方式執行,可以繼續輸入其他命令。
停止:

D:\hwy\nginx-1.8.0\nginx -s stop

重啟:

D:\hwy\nginx-1.8.0\nginx -s reload

二、配置單個server代理服務
為了模擬域名的形式訪問本地服務,我們修改windows的host檔案,新增

127.0.0.1 a.test.com
127.0.0.1 b.test.com #(待會配置多域名時使用)

在D:\hwy\nginx-1.8.0\conf目錄新增一個nginx-resin-a.conf,基本配置程式碼如下:

server{
        listen 80;
        server_name a.test.com;
        index index.html index.htm;
        root D:/hwy/aTest/src/main/webapp/;
        #配置首頁精確匹配
location = / { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://aTestServer; } #配置首頁 location / { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://aTestServer; } #動態頁面交給http://127.0.0.1:8080,也即我們之前在nginx.conf定義的upstream aTestServer 均衡 location ~ .*\.(php|jsp|cgi)?$ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://aTestServer; } #配置靜態資源字首匹配 location ~ ^/(fileUpload|doc)/ { root D:/hwy/www/aTest/; access_log off; expires 4d; } #配置Nginx動靜分離,定義的靜態頁面直接從專案指定目錄讀取。 location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { access_log off; expires 30d; } #定義Nginx輸出日誌的路徑 access_log D:/hwy/logs/aTest/access.log main; error_log D:/hwy/logs/aTest/error.log crit; }

將nginx-resin-a.conf引入到nginx.conf檔案裡面,nginx.conf如下:

#user  nobody;
#工作的子程序數量(通常等於CPU數量或者2倍於CPU)
worker_processes  4;

#錯誤日誌存放路徑  
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#指定pid存放檔案 
pid       logs/nginx.pid;          

worker_rlimit_nofile 51200;

events {
    #使用網路IO模型linux建議epoll,FreeBSD建議採用kqueue,window下不指定。  
    #use epoll; 
    #允許最大連線數  
    worker_connections  51200;
}

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  D:/hwy/nginx-1.8.0/logs/access.log  main;

    client_header_timeout  3m;
    client_body_timeout    3m;
    send_timeout           3m;

    client_header_buffer_size    1k;
    large_client_header_buffers  4 4k;

    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay     on;

    #keepalive_timeout  0;
    keepalive_timeout  120;

    upstream aTestServer {
        server 127.0.0.1:8080;
    }

    include  nginx-resin-a.conf;
}

好了,經過上邊的配置之後,我們啟動nginx以及本地你的aTest服務resin,訪問下http://a.test.com看看效果吧!

三、配置多域名
上邊配置了一個aTest的服務的代理,如果我們在伺服器上邊要執行多個服務,比如bTest服務,達到的效果是,通過http://a.test.com訪問aTest站點服務,通過http://b.test.com訪問bTest站點服務,其實也很簡單的,只需要在引入一個bTest的server配置即可。
在D:\hwy\nginx-1.8.0\conf目錄新增一個nginx-resin-b.conf,基本配置程式碼如下:

server{
        listen 80;
        server_name b.test.com;
        index index.html index.htm;
        root D:/hwy/bTest/src/main/webapp/;
        #配置首頁精確匹配
        location = / {
            proxy_next_upstream http_502 http_504 error timeout invalid_header;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://bTestServer;
        }
        #配置首頁
        location / {
            proxy_next_upstream http_502 http_504 error timeout invalid_header;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://bTestServer;
        }
        #動態頁面交給http://127.0.0.1:8090,也即我們之前在nginx.conf定義的upstream bTestServer 均衡
        location ~ .*\.(php|jsp|cgi)?$ {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://bTestServer;
        }
        #配置靜態資源字首匹配
        location ~ ^/(fileUpload|doc)/ {
            root D:/hwy/www/bTest/;
            access_log off;
            expires 4d;
        }
        #配置Nginx動靜分離,定義的靜態頁面直接從專案指定目錄讀取。
        location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
            access_log off;
            expires 30d;
        }

        #定義Nginx輸出日誌的路徑
        access_log D:/hwy/logs/bTest/access.log main;
        error_log D:/hwy/logs/bTest/error.log crit;
    }

然後我們只需要將nginx-resin-b.conf引入到nginx.conf配置檔案中即可,在nginx.conf的http最後邊增加

    upstream bTestServer {
        server 127.0.0.1:8090;
    }

    include  nginx-resin-b.conf;

好了,現在重啟nginx,並啟動本地你的bTest服務resin,分別訪問下http://a.test.comhttp://b.test.com看看是不是都到達指定的站點服務上去啦!

四、跨域配置
好了,現在我們有了兩個不同域名指定的專案了,但是現在bTest服務中有些介面資料請求需要由aTest來提供,bTest通過ajax請求aTest的介面資料,這個時候,如果直接請求,肯定是會涉及到跨域的問題了,瀏覽器是不允許的。現在我們可以通過nginx反向代理來實現跨域請求。

例項一:
在nginx-resin-b.conf配置中增加如下:

    location /api {
        rewrite ^.+api/?(.*)$ /api/$1 break;
        proxy_pass http://aTestServer;
    }

例項二:
在nginx-resin-b.conf配置中增加如下:

    location /baidu {
        rewrite ^.+baidu/?(.*)$ /$1 break;
        proxy_pass http://www.baidu.com;
    }

注意:這裡我們就把baidu網站整個搬到我們的127.0.0.1:8080/baidu/目錄下了,這樣我們訪問的時候,直接通過/baidu/xxx來請求百度的資料啦!

簡而言之,nginx 是通過把本地一個url字首對映到要跨域訪問的web伺服器上,就可以實現跨域訪問。

對於瀏覽器來說,訪問的就是同源伺服器上的一個url。而nginx通過檢測url字首,把http請求轉發到後面真實的物理伺服器,並通過rewrite命令重新指向真實的請求地址。這樣真實的伺服器就可以正確處理請求,並且並不知道這個請求是來自代理伺服器的。

簡單說,nginx伺服器欺騙了瀏覽器,讓它認為這是同源呼叫,從而解決了瀏覽器的跨域問題。又通過重寫url,欺騙了真實的伺服器,讓它以為這個http請求是直接來自與使用者瀏覽器的。