2017-02-11 3 views
0

私は同様の質問に行きましたが、成功しませんでした。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のドキュメントを見て、私はそれについての助けが見つかりませんでした。私はそれが正しい方法を理解する私の方法が正しいかどうか疑問に思います。

+0

これは誤植ですか: 'proxy_pass http:// localhost/diner:4000 /;'?ポートはドメイン名に接続する必要があります。 –

+0

@RichardSmithc私は 'proxy_pass http:// localhost:4000/diner /;'を実行すべきですか?私はちょうど試みたが、私はまだログに何も見ていないし、何とか400を返します。 'curl -X POST iamhungry.com/breakfast'が' http:// localhost:3000/breakfast /; 'に正しくリダイレ​​クトされているかどうか分かりません。 nginxの設定では正確ではありません。 – NoonanRosenblum

+0

'http:// localhost:4000;で始まり、アプリケーションに'/diner'をそのまま渡します。 'proxy_pass'は[here here](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass)です。 –

答えて

0

ありがとうございます@リチャードスミスです。

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. ドメイン名にポート番号が付けられています。ありがとう@リチャードスミス。

  3. @RichardSmithリンク:[...]ロケーションに一致する正規化されたリクエストURIの部分は、で指定されたURIに置き換えられます。

関連する問題