Node.js Express NGINX:静态文件path与名称间隔的proxy_pass断开

所以我正在访问我的服务器在domain.com:3333但只是把它切换到domain.com/nodeapp使用上游和代理传递在我的nginxconfiguration。 redirect工作得很好,但现在链接到我的index.html(/styles/style.css&socket.io/socket.io.ks)使用的静态文件被打破,所有显示的是原始的HTML。

从index.js

 app.get('/nodeapp', function(req,res){ res.sendFile(path.join(__dirname, '/views', 'index.html')); }); app.use('/styles', express.static(path.join(__dirname, '/views/styles'))); 

从index.html

 <link rel="stylesheet" type="text/css" href="styles/style.css"> <script src="socket.io/socket.io.js"></script> 

从… / nginx / sites-enabled / default

 upstream nodeapp{ server localhost:3333 fail_timeout=0; } server{ .... location /nodeapp{ proxy_pass http://nodeapp; } .... } 

我如何重新build立链接?

请注意,在我的index.js切换之前,显示的第一行是app.get('/', function(req,res){

所以,这是一个棘手的问题,但很简单。

 location /socket.io { proxy_pass http://nodeapp; } location /styles { proxy_pass http://nodeapp; } 

index.html链接的path必须在nginx conf中设置为prox_pass节点服务器的位置。

向你的位置和proxy_pass目标添加一个尾部斜线,让原来的index.js不变。 但是,您的链接应该指向index.html中的/ nodeapp / {something}。

 location /nodeapp/ { proxy_pass http://nodeapp/; }