【问题标题】:Nginx doesn't pass response headers of NodeJS ServerNginx 不传递 NodeJS 服务器的响应标头
【发布时间】:2021-12-03 15:12:38
【问题描述】:

我在配置我的 nginx 反向代理时遇到问题。就目前而言,请求如下所示:

root@devserver:~# curl -I https://example.com
HTTP/2 401
server: nginx
date: Fri, 15 Oct 2021 11:42:00 GMT
content-type: text/html; charset=utf-8
content-length: 172
www-authenticate: Basic realm="please login"
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
referrer-policy: no-referrer-when-downgrade
content-security-policy: default-src 'self' http: https: data: blob: 'unsafe-inline'; frame-ancestors 'self';
permissions-policy: interest-cohort=()
strict-transport-security: max-age=31536000; includeSubDomains



root@devserver:~# curl -I http://127.0.0.1:5000
HTTP/1.1 500 Internal Server Error
X-Powered-By: Express
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: *
Content-Security-Policy: default-src 'none'
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Content-Length: 1386
Date: Fri, 15 Oct 2021 11:42:52 GMT
Connection: keep-alive
Keep-Alive: timeout=5

Access-Control 标头丢失,我无法弄清楚如何配置 nginx 以将它们传递给浏览器。

我的 nginx 配置目前看起来像这样:

server {
    listen                  443 ssl http2;
    listen                  [::]:443 ssl http2;
    server_name             example.com;
    root                    /var/www/example.com;

    # SSL
    ...

    # reverse proxy
    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_http_version                 1.1;
        proxy_cache_bypass                 $http_upgrade;

        # Proxy headers
        proxy_set_header Upgrade           $http_upgrade;
        proxy_set_header Connection        $connection_upgrade;
        proxy_set_header Host              $host;
        proxy_set_header X-Real-IP         $remote_addr;
        proxy_set_header Forwarded         $proxy_add_forwarded;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host  $host;
        proxy_set_header X-Forwarded-Port  $server_port;

        # Proxy timeouts
        proxy_connect_timeout              60s;
        proxy_send_timeout                 60s;
        proxy_read_timeout                 60s;
    }
}

提前感谢您的帮助:)

【问题讨论】:

    标签: nginx cors nginx-reverse-proxy proxypass


    【解决方案1】:
    location / {
    proxy_pass https://127.0.0.1:80;
    proxy_set_header Host $host;
    proxy_hide_header X-Frame-Options;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Accept-Encoding "";
    client_body_timeout 3000;
    fastcgi_read_timeout 3000;
    client_max_body_size 128m;
    fastcgi_buffers 8 128k;
    fastcgi_buffer_size 128k;
    

    请使用这个并尝试。

    【讨论】:

    • 感谢您的建议,但响应没有变化。
    猜你喜欢
    • 2017-01-08
    • 1970-01-01
    • 2021-08-29
    • 2020-11-05
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多