nginx为什么所有路线都有效,只有一条是“301永久移动”?
我问了类似的问题,但没有成功 假设我有两个node.js应用程序正在打开服务器:nginx为什么所有路线都有效,只有一条是“301永久移动”?,nginx,http-status-code-301,Nginx,Http Status Code 301,我问了类似的问题,但没有成功 假设我有两个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) {
// 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!');
});
及
假设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的文档,但没有找到任何帮助。我不知道我对它工作原理的理解是否正确 好的,谢谢你@RichardSmith。我必须修复配置文件:
location /diner {
...
proxy_pass http://localhost:4000/diner;
用curl做测试http://iamhungry.com -I-L而不是curlhttp://iamhungry.com -我希望能真正跟随改道
以下是我所缺少的:
301不是错误。我在终端用curl做测试http://iamhungry.com -但是使用选项-L curlhttp://iamhungry.com -我-我允许我跟随重定向,然后到达终点!所以301使用nginx实际上是正常的,因为重定向是它的角色
端口号已附加到域名。谢谢@RichardSmith
从@RichardSmith链接:[…]匹配位置的规范化请求URI部分被指令中指定的URI替换
这是打字错误吗:代理通行证http://localhost/diner:4000/;? 端口应附加到域名。@RichardSmithc我应该做代理吗http://localhost:4000/diner/; ? 我只是试了一下,但我还是没有在日志中看到任何东西,它不知怎的返回了400。我不明白curl-X POST-iamHunger.com/早餐是如何正确重定向到的http://localhost:3000/breakfast/;. 在nginx配置中甚至不精确http://localhost:4000; 它将不经修改地传递给您的应用程序。代理通行证是
location /diner {
...
proxy_pass http://localhost:4000/diner;