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


轉載自:https://cloud.tencent.com/developer/article/1010639

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

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

一、nginx下載、安裝及啟動 下載地址:[nginx](http://nginx.org/en/download.html 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;
    }

注意:這里意思是將所有http://b.test.com/api/xxx類似的請求直接rewrite到http://a.test.com/api/xxx上邊去啦!

實例二: 在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請求是直接來自與用戶瀏覽器的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM