简体   繁体   English

Nginx + node.js配置

[英]Nginx + node.js configuration

I need the right configuration of nginx for my problem. 我需要为我的问题配置正确的Nginx。

Suppose the nginx + nodejs serverprograms are running on the same debian machine. 假设nginx + nodejs服务器程序在同一台debian计算机上运行。 Domain name for my website is for simplicity just webserver.com (and www.webserver.com as alias) 为简单起见,我网站的域名只是webserver.com(和www.webserver.com作为别名)

Now, when someone surfs on the internet to "webserver.com/" it should pass the request to the nodejs application which should run on a specific port like 3000 for example. 现在,当有人在Internet上浏览“ webserver.com/”时,它将把请求传递给应该运行在特定端口(例如3000)上的nodejs应用程序。 But the images and css files should get served by nginx as static files and the filestructure should looke like webserver.com/images or webserver.com/css .. images + css should get served by nginx like a static server 但是图像和css文件应由nginx作为静态文件提供服务,并且文件结构应类似于webserver.com/images或webserver.com/css ..图像+ css应由nginx如静态服务器提供服务

Now it gets tricky: But when someone surfs on webserver.com/staticsite001 or webserver.com/staticsite002 then it should get served by the nginx server only. 现在变得棘手:但是,当有人在webserver.com/staticsite001或webserver.com/staticsite002上冲浪时,则仅应由Nginx服务器提供服务。 no need for nodejs then. 然后不需要nodejs。

And for the nodejs server, I am just setting up my nodejs application with port 3000 for example to receive the bypass from nginx for webserver.com/ 对于nodejs服务器,我只是使用端口3000设置我的nodejs应用程序,例如,以接收来自nginx的绕过webserver.com/

to put it in a more understandable language: when someone surfs to webserver.com/staticsite001 it should NOT pass it to the node application. 用一种更易理解的语言来表示:当有人浏览webserver.com/staticsite001时,不应将其传递给节点应用程序。 It should only pass it to the node application if its inside of the first webserver.com/ directory that the outsiders can see. 仅当外部人可以看到它的第一个webserver.com/目录内部时,才应将其传递给节点应用程序。 The webserver.com/staticsite001 should only get serverd by nginx. webserver.com/staticsite001应该仅由nginx获得serverd。

How, how do I do that ? 如何,我该怎么做? And what should the http and server block look like for the nginx configuration look like? Nginx配置的http和server块应该是什么样?

I am familiar with nodejs. 我对nodejs很熟悉。 But I am new to nginx and new to reverse proxying. 但是我是nginx的新手,也是反向代理的新手。 thanks 谢谢

the file structure on the debian hard drive looks like: debian硬盘上的文件结构如下:

/home/wwwexample/staticsite001 (for www.webserver.com/staticsite001/) only handled by nginx /home/wwwexample/staticsite002 (for www.webserver.com/staticiste002/) only handlex by nginx /home/wwwexample/images / home / wwwexample / staticsite001(对于www.webserver.com/staticsite001/)仅由nginx处理/ home / wwwexample / staticsite002(对于www.webserver.com/staticiste002/)仅由nginx / home / wwwexample / images处理
/home/wwwexample/css /家庭/ wwwexample / CSS

and in /home/nodeapplication is my node js application 在/ home / nodeapplication中是我的node js应用程序

This server block should work: 此服务器块应工作:

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 {
    }
}

First location makes nginx to proxy everything to localhost:3000 . 第一个location使nginx将所有内容都代理localhost:3000 Following empty location s instruct nginx to use default behavior, that is to serve static files. 在空location之后,指示nginx使用默认行为,即提供静态文件。

Put this code into file /etc/nginx/sites-available/my-server and create a symlink to it in /etc/nginx/sites-enabled . 将此代码放入文件/etc/nginx/sites-available/my-server并在/etc/nginx/sites-enabled创建一个符号链接。 There is a default config, which you could use as a reference. 有一个default配置,您可以将其用作参考。

After that you could use command sudo /usr/sbin/nginx -t to check configuration. 之后,您可以使用命令sudo /usr/sbin/nginx -t检查配置。 If everything is OK use /etc/init.d/nginx reload to apply new configuration. 如果一切正常,请使用/etc/init.d/nginx reload来应用新配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM