【问题标题】:nginx why all routes works but one is "301 Moved Permanently "?nginx 为什么所有路由都有效,但一个是“301 Moved Permanently”?
【发布时间】:2017-06-29 11:38:19
【问题描述】:

我问过类似的问题,但没有成功。

假设我有两个 node.js 应用程序正在打开服务器:

// App GoodMorning
var express     = require('express');

app.post('/breakfast', function (req, res) {  
    console.log("Eating breakfast");
    res.sendStatus(200);
});

app.get('/', function (req, res) {
    res.send('GoodMorning');
});

app.listen(3000, function () {
  console.log('GoodMorning app listening on port 3000!');
});

// App GoodEvening
var express     = require('express');

app.post('/diner', function (req, res) {  
    console.log("Eating diner");
    res.sendStatus(200);
});

app.get('/', function (req, res) {
    res.send('GoodEvening');
});

app.listen(4000, function () {
  console.log('GoodEvening app listening on port 4000!');
});

假设 Nginx 被用作反向代理服务器。所以它必须将请求发送到正确的端口,对吗?所以“魔法”文件是这样的:

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


# HTTPS - proxy requests on to local Node.js app:
server {
    listen 443;
    server_name iamhungry.com;

    ssl on;
    # Use certificate and key provided by Let's Encrypt:
    ssl_certificate /etc/letsencrypt/live/iamhungry.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/iamhungry.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 / to localhost:3000:
    location / {
            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:3000/;
            proxy_ssl_session_reuse off;
            proxy_set_header Host $http_host;
            proxy_cache_bypass $http_upgrade;
            proxy_redirect off;
    }

    # Pass requests for /homepageevening to localhost:4000:
    location /homepageevening {
            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:4000/;
            proxy_ssl_session_reuse off;
            proxy_set_header Host $http_host;
            proxy_cache_bypass $http_upgrade;
            proxy_redirect off;
    }

    # Pass requests for /diner to localhost/diner:4000:
    location /diner {
            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/diner:4000/;
            proxy_ssl_session_reuse off;
            proxy_set_header Host $http_host;
            proxy_cache_bypass $http_upgrade;
            proxy_redirect off;
    }

}

那么下面的请求会产生如下的结果:

$ curl iamhungry.com 
$ GoodMorning // OK

$ curl -X POST iamhungry.com/breakfast
--> I see "Eating brakfast" in the log file of breakfast.js // OK


$ curl iamhungry.com/homepageevening
$ GoodEvening // OK

$ curl -X POST iamhungry.com/diner -I
HTTP/1.1 301 Moved Permanently // why ?!
--> And I see nothing in the log file of evening.js // why ?!

我对这些代理概念并不放心。我浏览了 nginx 的文档,但没有找到任何帮助。我想知道我理解它的工作方式的方式是否正确。

【问题讨论】:

  • 这是一个错字:proxy_pass http://localhost/diner:4000/;?端口应附加到域名。
  • @RichardSmithc 我应该做proxy_pass http://localhost:4000/diner/; 吗?我刚刚尝试过,但我仍然没有在日志中看到任何内容,并且它以某种方式返回 400。我不明白curl -X POST iamhungry.com/breakfast 是如何正确重定向到http://localhost:3000/breakfast/; 的。在 nginx 配置中甚至都不精确。
  • http://localhost:4000; 开头,它将未经修改地将/diner 传递给您的应用程序。 proxy_passdocumented here

标签: nginx http-status-code-301


【解决方案1】:

好的,谢谢@RichardSmith。我必须修复配置文件:

  location /diner {
        ...
        proxy_pass http://localhost:4000/diner;

并使用curl http://iamhungry.com -I -L 而不是curl http://iamhungry.com -I 进行测试,以实际遵循重新路由。

这是我所缺少的:

  1. 301 不是错误。我正在使用curl http://iamhungry.com -I 在终端中进行测试,但使用选项 -L curl http://iamhungry.com -I -L 允许我遵循重定向然后到达行尾!所以 301 其实使用 nginx 是正常的,因为重定向是它的作用。

  2. 端口号附加到域名。谢谢@RichardSmith。

  3. 来自@RichardSmith 链接:[...]与位置匹配的规范化请求 URI 部分被指令中指定的 URI 替换

【讨论】:

    猜你喜欢
    • 2017-02-28
    • 2017-06-13
    • 2017-01-01
    • 2014-06-30
    • 2012-01-09
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多