cookie 跨域问题
生活随笔
收集整理的這篇文章主要介紹了
cookie 跨域问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
cookie 跨域訪問
一、 前言
二、 cookie介紹
cookie路徑 :
path表示cookie所在的目錄,asp.net默認為/,就是根目錄。在同一個服務器上有目錄如下:/test/,/test/cd/,/test/dd/,現設一個cookie1的path為/test/,cookie2的path為/test/cd/,那么test下的所有頁面都可以訪問到cookie1,而/test/和/test/dd/的子頁面不能訪問cookie2。這是因為cookie能讓其path路徑下的頁面訪問。
document.cookie = "name = value; path=/";三、 解決cookie跨域問題之Nginx反向代理
場景模擬
nginx配置如下:
worker_processes 2; events {worker_connections 65535; } 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"';server_names_hash_bucket_size 128;client_header_buffer_size 32k;large_client_header_buffers 4 32k;client_body_buffer_size 8m;server_tokens off;ignore_invalid_headers on;recursive_error_pages on;server_name_in_redirect off;sendfile on;tcp_nopush on;tcp_nodelay on;#keepalive_timeout 0;keepalive_timeout 65;upstream web1{server 127.0.0.1:8089 max_fails=0 weight=1;}upstream web2 {server 127.0.0.1:8080 max_fails=0 weight=1;}server {listen 80;server_name 127.0.0.1;charset utf-8;index index.html;location /web/web1 {proxy_pass http://web1/web1;proxy_set_header Host 127.0.0.1;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header Cookie $http_cookie;log_subrequest on;}location /web/web2 {proxy_pass http://web2/web2;proxy_set_header Host 127.0.0.1;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header Cookie $http_cookie;log_subrequest on;}location /nginxstatus {stub_status on;access_log on;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}} }總結 :
利用nginx的方向代理來解決cookie跨域問題,其實是通過“欺騙”瀏覽器來實現的,通過nginx,我們可以將不同工程的cookie放到nginx域下,通過nginx反向代理就可以取到不同工程寫入的cookie。其實上述場景中 $.cookie(“user”, “hjzgg”, {path: “/web”}); 中的path可以寫成 “/”, 這樣nginx的配置就更為簡單了,如下。
location /web1 {proxy_pass http://web1;proxy_set_header Host 127.0.0.1;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header Cookie $http_cookie;log_subrequest on;}location /web2 {proxy_pass http://web2;proxy_set_header Host 127.0.0.1;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header Cookie $http_cookie;log_subrequest on;}四 、解決cookie跨域問題之JSONP請求
五、解決cookie跨域問題之nodejs superagent
六、同一域下,不同工程下的cookie攜帶問題
總結
以上是生活随笔為你收集整理的cookie 跨域问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracel 中序列
- 下一篇: GitLab私服搭建及使用实践