場景
- 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/
這樣就不會跨域了。