我去过类似的问题,但没有取得任何成功。nginx为什么所有路线都有效,但其中一条是“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) {
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配置中甚至不精确。 – NoonanRosenblum
以'http:// localhost:4000;'开头,它会将'/ diner'传递给你的应用程序。 'proxy_pass' [此处有记录](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass) –