[英]Nginx + node.js configuration
我需要为我的问题配置正确的Nginx。
假设nginx + nodejs服务器程序在同一台debian计算机上运行。 为简单起见,我网站的域名只是webserver.com(和www.webserver.com作为别名)
现在,当有人在Internet上浏览“ webserver.com/”时,它将把请求传递给应该运行在特定端口(例如3000)上的nodejs应用程序。 但是图像和css文件应由nginx作为静态文件提供服务,并且文件结构应类似于webserver.com/images或webserver.com/css ..图像+ css应由nginx如静态服务器提供服务
现在变得棘手:但是,当有人在webserver.com/staticsite001或webserver.com/staticsite002上冲浪时,则仅应由Nginx服务器提供服务。 然后不需要nodejs。
对于nodejs服务器,我只是使用端口3000设置我的nodejs应用程序,例如,以接收来自nginx的绕过webserver.com/
用一种更易理解的语言来表示:当有人浏览webserver.com/staticsite001时,不应将其传递给节点应用程序。 仅当外部人可以看到它的第一个webserver.com/目录内部时,才应将其传递给节点应用程序。 webserver.com/staticsite001应该仅由nginx获得serverd。
如何,我该怎么做? Nginx配置的http和server块应该是什么样?
我对nodejs很熟悉。 但是我是nginx的新手,也是反向代理的新手。 谢谢
debian硬盘上的文件结构如下:
/ home / wwwexample / staticsite001(对于www.webserver.com/staticsite001/)仅由nginx处理/ home / wwwexample / staticsite002(对于www.webserver.com/staticiste002/)仅由nginx / home / wwwexample / images处理
/家庭/ wwwexample / CSS
在/ home / nodeapplication中是我的node js应用程序
此服务器块应工作:
server {
listen 80;
server_name webserver.com www.webserver.com;
root /home/wwwexample;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
}
location /staticsite001 {
}
location /staticsite002 {
}
location /images {
}
location /css {
}
}
第一个location
使nginx将所有内容都代理到localhost:3000
。 在空location
之后,指示nginx使用默认行为,即提供静态文件。
将此代码放入文件/etc/nginx/sites-available/my-server
并在/etc/nginx/sites-enabled
创建一个符号链接。 有一个default
配置,您可以将其用作参考。
之后,您可以使用命令sudo /usr/sbin/nginx -t
检查配置。 如果一切正常,请使用/etc/init.d/nginx reload
来应用新配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.