V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
firefox12
V2EX  ›  程序员

求助一个 nginx 代理的问题 好像用了代理以后 就不太对了 求前端大神看一眼, AngularJS 不能用 nginx 代理?

  •  
  •   firefox12 · 2018-12-22 23:37:20 +08:00 · 1362 次点击
    这是一个创建于 1945 天前的主题,其中的信息可能已经有所发展或是发生改变。
    问题是这样,在 搭建完一个 k8s 的 dashboard 以后,大家可以用浏览器访问它,但是因为证书问题,大多数浏览器都不允许访问,现在的浏览器连允许访问的机会都不给,所以 从实际出发我们需要解决这个问题,

    我尝试了 2 种办法,第一种 写了一个 tcp 代理, 代理 和 浏览器之间是有普通 tcp, 代理和 dashboard 之间用了 ssl, 这样浏览器可以不加 https:// 直接访问网页,网页是对了 但是当我准备提交数据 登录之后,就卡住了。可能我的代理写得有问题,我只能这么认为,所以暂时放弃

    方法二 利用 nginx 反向代理,我觉得这个应该是可以的,但是遭遇了一样的故障,无法登陆。为此 我在 firefox 里把登录的详情记录下来, 请大神看看 故障到底在哪里。

    首先 dashboard 应该是 AngularJS 的东西

    /**
    * @license AngularJS v1.6.6
    * (c) 2010-2017 Google, Inc. http://angularjs.org
    * License: MIT
    */

    问题出在 登录的这一步

    情况 1 直接访问 https://192.168.3.11:9919 忽略掉证书后 可以正常登录
    它在 POST login 后返回这样内容
    content-encoding gzip
    content-length 1485
    content-type application/json
    date Sat, 22 Dec 2018 15:02:38 GMT
    X-Firefox-Spdy h2
    请求头 (541 字节)
    Accept application/json, text/plain, */*
    Accept-Encoding gzip, deflate, br
    Accept-Language zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
    Cache-Control no-cache
    Connection keep-alive
    Content-Length 3038
    Content-Type application/json;charset=utf-8
    DNT 1
    Host 192.168.3.11:9919
    Pragma no-cache
    Referer https://192.168.3.11:9919/
    User-Agent Mozilla/5.0 (Windows NT 6.1; W …) Gecko/20100101 Firefox/57.0
    X-CSRF-TOKEN zVX9g720Tk083N0e4X5akME4lhs:1545490958151

    它返回一个 3038 字节的 json jweToken=。。。。。


    如果使用 nginx 代理访问 返回内容是这样

    Connection keep-alive
    Content-Encoding gzip
    Content-Length 1485
    Content-Type application/json
    Date Sat, 22 Dec 2018 15:10:00 GMT
    Server nginx/1.12.2
    请求头 (526 字节)
    Accept application/json, text/plain, */*
    Accept-Encoding gzip, deflate
    Accept-Language zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
    Cache-Control no-cache
    Connection keep-alive
    Content-Length 3038
    Content-Type application/json;charset=utf-8
    DNT 1
    Host 192.168.3.11
    Pragma no-cache
    Referer http://192.168.3.11/
    User-Agent Mozilla/5.0 (Windows NT 6.1; W …) Gecko/20100101 Firefox/57.0
    X-CSRF-TOKEN tXReHYcrGt66CgwoQRwhka8PtaU:1545491400386

    我感觉除了 referer 其他都一样,3038d 的内容也是一个 json

    但是下一步就完全不一样, 直接利用 https:// 访问的情况, 新的请求上会有 cookie 并且把 jswtoken 带上,而用了 nginx 代理的就没有, 因为 这个 cookie 不是靠 http head 来设置的,应该是 js 解析了内容后做的,难道用了代理以后 js 工作就不正常了?

    nginx 代理配置如下


    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log debug;
    pid /run/nginx.pid;

    # Load dynamic modules. See /usr/share/nginx/README.dynamic.
    include /usr/share/nginx/modules/*.conf;

    events {
    worker_connections 1024;
    }

    http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';


    #access_log /var/log/nginx/access.log main;
    access_log /var/log/nginx/access.log main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    root /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    # location / {
    # }




    location / {
    #proxy_redirect off;
    #proxy_set_header Host $host;
    #proxy_set_header X-Real-IP $remote_addr;
    #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;



    proxy_set_header Host $host;
    proxy_set_header Referer $http_referer;
    proxy_set_header Cookie $http_cookie;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass_request_body on;
    #`proxy_cookie_domain https://192.168.3.11;
    proxy_ssl_verify off;
    proxy_pass https://192.168.3.11:9919/; # 这里设置你要代理的 ip+端口



    proxy_redirect default;

    }



    error_page 404 /404.html;
    location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }
    }


    求指导!
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2897 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 02:48 · PVG 10:48 · LAX 19:48 · JFK 22:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.