V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
tudage
V2EX  ›  问与答

小弟请教一个 Nginx 反向代理解决跨域的问题

  •  
  •   tudage · 2018-08-06 20:28:27 +08:00 · 1325 次点击
    这是一个创建于 2081 天前的主题,其中的信息可能已经有所发展或是发生改变。

    本地跨域配置是 proxy,本地测试跨域没有问题,数据能正常接受,后台能 console

      "/api": {
        "target": "http://132.232.22.140:8889/api",
        "changeOrigin": true,
        "pathRewrite": { "^/api" : "" }
      }
    

    然后部署到服务器后,在网上看了一下用 nginx 配置跨域

    server {
            listen 8000;
            server_name localhost;
    
            root /home/myftp/dist;
            index index.html index.htm;
    
            location / {
                    try_files $uri $uri/ /index.html;
            }
            location ^~ /assets/ {
                    gzip_static on;
                    expires max;
                    add_header Cache-Control public;
            }
    location /api {
        rewrite  ^.+api/?(.*)$ /$1 break;
        include  uwsgi_params;
           proxy_pass   http://localhost:8889;
           }
            error_page 500 502 503 504 /500.html;
            client_max_body_size 20M;
            keepalive_timeout 10;
    }
    

    然后在线上测试是接口 404,但是本地测试线上的数据库接口又是通的,求大大们支支招

    6 条回复    2018-08-07 23:09:09 +08:00
    tudage
        1
    tudage  
    OP
       2018-08-06 20:58:59 +08:00
    求大大们看看呀
    silverfox
        2
    silverfox  
       2018-08-07 09:35:57 +08:00
    这个错误跟跨域没有关系,请检查 Nginx 日志,应该是 rewrite 后的路径在后端服务不存在。
    tudage
        3
    tudage  
    OP
       2018-08-07 13:14:16 +08:00
    @silverfox 看了 success 和 error 日志,没有任何相关记录,后端服务接口我还写了一个 console,也 console 不出来,但是本地测试就可以,应该是没有配置对,但是小弟是照着网上给得配置得,都是一样得 rewrite。。
    silverfox
        4
    silverfox  
       2018-08-07 14:57:10 +08:00
    我觉得不太可能没有日志,如果 access 里都没有客户端访问 /api/xxx 的日志,那只能说客户端调用的不是这台服务器。

    另一种可能是 Nginx 针对这个 server 或 location 不记录日志。
    tudage
        5
    tudage  
    OP
       2018-08-07 15:57:15 +08:00
    @silverfox 应该是请求没有落在这个端口上面,就是配置不对,但是网上都是这么写得啊。。。
    tudage
        6
    tudage  
    OP
       2018-08-07 23:09:09 +08:00
    还是没有解决
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2880 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 00:25 · PVG 08:25 · LAX 17:25 · JFK 20:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.