如何configurationnginx,使其与Express一起工作?

我正在尝试configurationnginx,以便对我的节点应用程序进行proxy_pass请求。 StackOverflow的问题得到了许多upvotes: https ://stackoverflow.com/questions/5009324/node-js-nginx-and-now,我使用从那里的configuration。

(但由于问题是关于服务器configuration,它应该是在ServerFault上)

这里是nginxconfiguration:

 server { listen 80; listen [::]:80; root /var/www/services.stefanow.net/public_html; index index.html index.htm; server_name services.stefanow.net; location / { try_files $uri $uri/ =404; } location /test-express { proxy_pass http://127.0.0.1:3002; } location /test-http { proxy_pass http://127.0.0.1:3003; } } 

使用普通节点:

 var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(3003, '127.0.0.1'); console.log('Server running at http://127.0.0.1:3003/'); 

有用! 检查: http : //services.stefanow.net/test-http

使用快递:

 var express = require('express'); var app = express(); // app.get('/', function(req, res) { res.redirect('/index.html'); }); app.get('/index.html', function(req, res) { res.send("blah blah index.html"); }); app.listen(3002, "127.0.0.1"); console.log('Server running at http://127.0.0.1:3002/'); 

它不起作用:(请参阅: http : //services.stefanow.net/test-express


我知道有什么事情正在发生。

a)test-express没有运行 在这里输入图像描述

b)文本快递正在运行

在这里输入图像描述

(我可以确认它正在运行通过命令行,而在服务器上的SSH)

 root@stefanow:~# service nginx restart * Restarting nginx nginx [ OK ] root@stefanow:~# curl localhost:3002 Moved Temporarily. Redirecting to /index.html root@stefanow:~# curl localhost:3002/index.html blah blah index.html 

我尝试设置标题如下所述: http : //www.nginxtips.com/how-to-setup-nginx-as-proxy-for-nodejs/ (仍然不起作用)

 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; 

我也尝试用'localhost'replace'127.0.0.1',反之亦然


请指教。 我很确定我错过了一些明显的细节,我想了解更多。 谢谢。

你expressionconfiguration为服务path/index.html ,但你需要/test-express/index.html 。 可以configurationexpress来服务/test-express/index.html或者让nginx从代理请求中/test-exress 。 后者就像添加尾随斜杠到locationproxy_pass一样简单。

 location /test-express/ { proxy_pass http://127.0.0.1:3002/; } 

有关详细信息,请参阅http://nginx.org/r/proxy_pass