1. 程式人生 > >Nginx反向代理配置(解決跨域問題)

Nginx反向代理配置(解決跨域問題)

一. 跨域

   指的是瀏覽器不能執行其他網站的指令碼。它是由瀏覽器的同源策略造成的,是瀏覽器對 JavaScript 施加的安全限制。

實際開發過程中表現為,如果本地的Html程式碼未提交到伺服器,本地是不能直接呼叫伺服器 API 獲取資料的。

二. Nginx

Nginx 是一個高效能的 HTTP 和反向代理伺服器,也是一個IMAP/POP3/SMTP 伺服器。其特點是佔有記憶體少,併發能力強,事實上 nginx 的併發能力確實在同類型的網頁伺服器中表現較好,中國大陸使用 nginx 網站使用者有:百度、京東、新浪、網易、騰訊、淘寶等。

三. 反向代理

反向代理(Reverse Proxy)方式是指以代理伺服器來接受internet上的連線請求,然後將請求轉發給內部網路上的伺服器,並將從伺服器上得到的結果返回給internet上請求連線的客戶端,此時代理伺服器對外就表現為一個反向代理伺服器。

四. 利用反向代理解決跨域問題的實際例子

在訪問本地http://localhost:9966/lujing/#/basicData/carList頁面時,會呼叫一個介面http://localhost:9966/antColonyTmcsApi/ltsVehicle/listVehicle?_t=1539742798083&pageNo=1&pageSize=10查詢車輛列表資料。

因為涉及到跨域問題,所以是調不通的。

解決辦法:

 在nginx.conf經過下面的配置後(如最後的配置程式碼),就可以訪問遠端伺服器上的介面資料了,即解決了跨域問題。

通過下面的兩句配置,可以訪問 antColonyUmsApi和antColonyTmcsApi遠端路徑下面的URL

#  antColonyUmsApi 這個路徑下面的api
        location /antColonyUmsApi

#  antColonyTmcsApi 這個路徑下面的api(http://localhost:9966/antColonyTmcsApi/ltsVehicle/listVehicle?_t=1539742798083&pageNo=1&pageSize=10

location /antColonyTmcsApi 

nginx.conf配置程式碼如下:

 server {
        listen       9966;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        
        #  antColonyUmsApi 這個路徑下面的api
        location /antColonyUmsApi {
        if ($request_method = 'OPTIONS') { 
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
            #
            # Custom headers and headers various browsers *should* be OK with but aren't
            #
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
            #
            # Tell client that this pre-flight info is valid for 20 days
            #
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }


        if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' *; 
            add_header 'Access-Control-Allow-Credentials' 'true'; 
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }


        if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' *; 
            add_header 'Access-Control-Allow-Credentials' 'true'; 
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
        # 匹配apis之後的路徑和引數
        rewrite  ^.+apis/?(.*)$ /$1 break;
        include  uwsgi_params;
        # 實際呼叫的API
        proxy_pass http://statictest.tflj.com;
       }

       #  antColonyTmcsApi 這個路徑下面的api
       location /antColonyTmcsApi {
        if ($request_method = 'OPTIONS') { 
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
            #
            # Custom headers and headers various browsers *should* be OK with but aren't
            #
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
            #
            # Tell client that this pre-flight info is valid for 20 days
            #
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }


        if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' *; 
            add_header 'Access-Control-Allow-Credentials' 'true'; 
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }


        if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' *; 
            add_header 'Access-Control-Allow-Credentials' 'true'; 
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
        # 匹配apis之後的路徑和引數
        rewrite  ^.+apis/?(.*)$ /$1 break;
        include  uwsgi_params;
        # 實際呼叫的API
        proxy_pass http://statictest.tflj.com;
       }

        #error_page  404              /404.html;