【问题标题】:nginx not matching location (I think)nginx 不匹配位置(我认为)
【发布时间】:2016-03-21 21:33:26
【问题描述】:

我正在根据本文档将解析服务器迁移到 DigitalOcean Ubuntu 液滴:

https://www.digitalocean.com/community/tutorials/how-to-migrate-a-parse-app-to-parse-server-on-ubuntu-14-04

我在向解析服务器发送 nginx 代理 (https) 请求时遇到问题。

如果我在我的 droplet 上打开端口 1337 并将 Parse http 请求配置为直接转到 parse-server (http://example.com:1337/parse),这一切都会按预期工作。

如果我将 Parse http 请求配置为转到 https://example.com/parse,则 nginx 无法将请求代理到 parse-server。

nginx 返回 404(这可能是 / 位置的作用)。

Let's Encrypt SSL 证书似乎按预期工作,因为我可以提供静态内容(http 请求通过 301 状态从 80 重定向到 443)。

我的 nginx 默认配置如下:

# HTTP - redirect all requests to HTTPS
server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;
    return 301 https://$host$request_uri;
}

# HTTPS - serve HTML from /usr/share/nginx/html, proxy requests to /parse/
# through to Parse Server
server {
    listen 443;
    server_name example.com;

    root /usr/share/nginx/html;
    index index.html index.htm;

    ssl on;
    # Use certificate and key provided by Let's Encrypt:
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

    # Pass requests for /parse/ to Parse Server instance at localhost:1337
    location /parse/ {
            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_pass http://localhost:1337/;
            proxy_ssl_session_reuse off;
            proxy_set_header Host $http_host;
            proxy_redirect off;
    }

    location / {
            try_files $uri $uri/ =404;
    }
}

这是来自 nginx access.log 的典型行:

xxx.xxx.xxx.xxx - - [20/Mar/2016:18:54:53 -0400] "PUT /parse/classes/_User/xxxxxxxxxx HTTP/1.1" 404 68 "-" ...

有什么方法可以打开更详细的调试,以便我知道匹配失败的原因吗?

谢谢, 彼得

解决方案(见下文) 注意在proxy_pass配置中添加/parse/

  location /parse/ {
            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_pass http://localhost:1337/parse/;
            proxy_ssl_session_reuse off;
            proxy_set_header Host $http_host;
            proxy_redirect off;
    }

【问题讨论】:

    标签: ubuntu parse-platform nginx digital-ocean parse-server


    【解决方案1】:

    您的配置不代理https://example.com/parse,它代理https://example.com/parse/。前一个 URI 由 location / 块处理。

    此外,URI https://example.com/parse/ 被上游发送到http://example.com:1337/,而不是http://example.com:1337/parse

    如果您需要透明地代理不带/ 的 URI,请使用:

    location /parse {
        proxy_pass http://localhost:1337;
        ...
    }
    

    请注意,两个尾随 /s 已被删除。详情请见this document

    【讨论】:

    • 非常感谢@Richard。我的解决方案是将位置保留为 /parse/,但通过在末尾添加 /parse/ 来更改 proxy_pass URL。请参阅上面的解决方案更新。
    猜你喜欢
    • 1970-01-01
    • 2011-07-11
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    相关资源
    最近更新 更多