2017-02-11 14 views
0

我去過類似的問題,但沒有取得任何成功。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的文檔,結果沒有找到任何幫助。我想知道我的理解方式是否正確。

+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' [此處有記錄](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass) –

回答

0

好的謝謝@RichardSmith。我不得不修復的配置文件:

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

,做我的測試與curl http://iamhungry.com -I -L而不是curl http://iamhungry.com -I實際遵循重新路由。

這裏是我缺少的是什麼:

  1. A 301是不是一個錯誤。我正在使用curl http://iamhungry.com -I在終端上做我的測試,但使用選項-L curl http://iamhungry.com -I -L允許我按照重定向進行操作,然後結束這一行!所以301使用nginx實際上是正常的,因爲重定向是它的作用。

  2. 端口號被附加到域名。謝謝@RichardSmith。

  3. 來自@RichardSmith鏈接:匹配位置的標準化請求URI部分被指令中指定的URI替換。

相關問題