【问题标题】:nginx proxy is not working using subdomainnginx 代理无法使用子域
【发布时间】:2019-05-08 05:50:36
【问题描述】:

我有两个域

alpha.mydomain.comapi-alpha.mydomain.com

我正在尝试使用 nginx 作为代理

我收到了错误

在“https://api-alpha.mydomain.com/dup-check”访问 XMLHttpRequest 来自原点 'https://alpha.mydomain.com' 已被 CORS 阻止 策略:对预检请求的响应未通过访问控制 检查:没有“Access-Control-Allow-Origin”标头出现在 请求的资源。

我认为根据我的设置,请求不应该使用 api-alpha.mydomain.com 而是 127.0.0.1(并且没有收到 CORS 错误)

注意:: 我使用的是 cloudflare https,所以控制台错误是 https,cloudflare 是 SSL,并且与我的 nginx 服务器的 80 端口通信

这是我的 nginx 配置的一部分

   server {
        listen        80;
        server_name   alpha.mydomain.com ;

        access_log   /var/log/nginx.access_log  main;

        root /home/mydomain/react-front/dist;

        location / {
            try_files $uri $uri/ /index.html;
        }

    }
   server {
        listen        80;
        server_name   api-alpha.mydomain.com ;

        access_log   /var/log/nginx-api-alpha-access.log  main;

        location /{
                proxy_pass         http://127.0.0.1:4001/;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_ssl_session_reuse off;
                proxy_set_header Host $http_host;
                proxy_redirect off;
        }

    }

这是来自 nginx-api-alpha-access.log 的条目

"OPTIONS /dup-check HTTP/1.1" 502 750 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36" " -”

这是来自 /var/log/nginx/error.log 的条目

[error] 1280#1280: *12 connect() failed (111: Connection denied) while connection to upstream, client: 172.xx.xxx.xxx, server: api-mydomain.trigfig.com, request: "选项 /dup-check HTTP/1.1",上游:"http://127.0.0.1:4001/dup-check",主机:"api-alpha.mydomain.com"

谢谢,不知道我的配置中缺少什么

【问题讨论】:

    标签: nginx proxy


    【解决方案1】:

    尝试更改为

    server {
            listen        80;
            server_name   alpha.mydomain.com ;
    
            access_log   /var/log/nginx.access_log  main;
    
            root /home/mydomain/react-front/dist;
    
            location / {
                try_files $uri $uri/ /index.html;
            }
    
        }
       server {
            listen        80;
            server_name   api-alpha.mydomain.com ;
    
            access_log   /var/log/nginx-api-alpha-access.log  main;
    
            location /{
    
                if ($request_method = 'OPTIONS') {
                    add_header 'Access-Control-Allow-Origin' '*';
                    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
                    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';
                    add_header 'Access-Control-Max-Age' 1728000;
                    add_header 'Content-Type' 'text/plain; charset=utf-8';
                    add_header 'Content-Length' 0;
                    return 204;
                 }
    
                 add_header 'Access-Control-Allow-Origin' '*';
                 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
                 add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';
                 add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range,Authorization';
    
                    proxy_pass         http://127.0.0.1:4001/;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header X-NginX-Proxy true;
                    proxy_ssl_session_reuse off;
                    proxy_set_header Host $http_host;
                    proxy_redirect off;
            }
    
        }
    

    【讨论】:

    • 我认为add_header 'Access-Control-Allow-Origin' 'alpha.mydomain.com'; 对于这种情况应该足够了。
    猜你喜欢
    • 2021-12-18
    • 2016-07-29
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 2016-08-19
    • 2022-11-28
    相关资源
    最近更新 更多