日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

nginx配置反向代理解决前后端分离跨域问题

發(fā)布時間:2024/4/17 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nginx配置反向代理解决前后端分离跨域问题 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

摘自《AngularJS深度剖析與最佳實踐》P132

nginx配置文件如下:

server {listen 80;server_name your.domain.name;location / {# 把跟路徑下的請求轉(zhuǎn)發(fā)給前端工具鏈(如gulp)打開的開發(fā)服務器# 如果是產(chǎn)品環(huán)境,則使用root等指令配置為靜態(tài)文件服務器proxy_pass http://localhost:5000/;}location /api/ {# 把 /api 路徑下的請求轉(zhuǎn)發(fā)給真正的后端服務器proxy_pass http://localhost:8080/service/;# 把host頭傳過去,后端服務程序?qū)⑹盏統(tǒng)our.domain.name, 否則收到的是localhost:8080proxy_set_header Host $http_host;# 把cookie中的path部分從/api替換成/serviceproxy_cookie_path /api /service;# 把cookie的path部分從localhost:8080替換成your.domain.nameproxy_cookie_domain localhost:8080 your.domain.name }}

配置完成后重啟nginx服務:

nginx -s reload

?

flying get√ 總是要拼了命繼續(xù)努力

?目前很多網(wǎng)站都是用前后端完全分離的模式實現(xiàn),即:后端通過API提供數(shù)據(jù),前端使用API獲取數(shù)據(jù)并渲染。不過這樣做會存在API跨域的問題,這里介紹一種通過Nginx配置解決跨域問題的方法。

?

????Nginx整體配置如下:

upstream service {server 127.0.0.1:8080; } map $http_origin $cors_header {default "";"~^https?://localhost(:[0-9]+)?$" "$http_origin"; }server {listen 80;server_name 127.0.0.1;access_log /var/log/nginx/access.log;error_log /var/log/nginx/error.log;location = /favicon.ico { deny all; error_log off; access_log off; log_not_found off; }location /api/ {if ($request_method = 'OPTIONS') {add_header 'Content-Length' 0 always;add_header 'Content-Type' 'text/plain charset=UTF-8' always;add_header 'Access-Control-Allow-Origin' '$cors_header' always;add_header 'Access-Control-Allow-Credentials' 'true' always;add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;add_header 'Access-Control-Allow-Headers' 'Origin,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Accept,Cookie,Set-Cookie, X-AUTH-USER, X-AUTH-TOKEN' always;return 200;}if ($request_method = 'POST') {add_header 'Access-Control-Allow-Origin' '$cors_header' always;add_header 'Access-Control-Allow-Credentials' 'true' always;add_header 'Access-Control-Allow-Headers' 'Origin,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Accept,Cookie,Set-Cookie, X-AUTH-USER, X-AUTH-TOKEN' always;add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;}if ($request_method = 'GET') {add_header 'Access-Control-Allow-Origin' '$cors_header' always;add_header 'Access-Control-Allow-Credentials' 'true' always;add_header 'Access-Control-Allow-Headers' 'Origin,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Accept,Cookie,Set-Cookie, X-AUTH-USER, X-AUTH-TOKEN' always;add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;}uwsgi_pass service;include uwsgi_params;} }

? ? API路徑配置中的 "X-AUTH-USER, X-AUTH-TOKEN",是API中傳遞的自定義HEADER,需要在 "Access-Control-Allow-Headers"中指明。????其中,"map $http_origin $cors_header"將需要跨域的域名或者IP解析出來,方便后面的配置處理。

轉(zhuǎn)載于:https://my.oschina.net/u/930306/blog/769840

總結(jié)

以上是生活随笔為你收集整理的nginx配置反向代理解决前后端分离跨域问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。