1. 程式人生 > >nginx解決跨域問題

nginx解決跨域問題

場景

  • A:是放在nginx/html 下的html頁面 屬於靜態資源! (不是用tomcat釋出過的)
  • B:是部署在本伺服器上的介面層。開放埠為8007
  • 目的:A.html 裡面 寫個ajax -----》請求到B的某個埠 比如 8007 下的方法。

很多方法可以解決。網上找了一下。
很多都是前端允許攜帶cookie,後端再配個過濾器,允許跨域訪問。

而我這個B 他是介面層的。(給app端提供呼叫) 怎麼配過濾器。。(樓住知識有限)

解決辦法:

開啟nginx/conf/nginx.conf

新增如下程式碼:

location /wkzs-restful {
	    root   html;
            proxy_pass    http://localhost:8007/;
   
            proxy_set_header           Host $host;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size  100m;
            index    index.html ind ml index.htm;
  
        }

這裡寫圖片描述

意思:

當你從外部訪問 http://此伺服器的ip/wkzs-restful/login.do
相當於此伺服器訪問 http://localhost:8007/login.do

這樣就不會跨域了。

需要注意的是,域名訪問和ip訪問依然會跨域。寫成一致就好。

有疑問的或者有知道解決方法的。歡迎留言!

這裡寫圖片描述