私は同様の質問に行きましたが、成功しませんでした。nginxなぜすべてのルートが動作しますが、1つは「301 Moved Permanently」ですか?
レッツは、私は、サーバーの電源を入れる2つの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'は[here here](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass)です。 –