简体   繁体   中英

Nginx + node.js configuration

I need the right configuration of nginx for my problem.

Suppose the nginx + nodejs serverprograms are running on the same debian machine. Domain name for my website is for simplicity just webserver.com (and www.webserver.com as alias)

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. 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

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. no need for nodejs then.

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/

to put it in a more understandable language: when someone surfs to webserver.com/staticsite001 it should NOT pass it to the node application. It should only pass it to the node application if its inside of the first webserver.com/ directory that the outsiders can see. The webserver.com/staticsite001 should only get serverd by nginx.

How, how do I do that ? And what should the http and server block look like for the nginx configuration look like?

I am familiar with nodejs. But I am new to nginx and new to reverse proxying. thanks

the file structure on the debian hard drive looks like:

/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/css

and in /home/nodeapplication is my node js application

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 . Following empty location s instruct nginx to use default behavior, that is to serve static files.

Put this code into file /etc/nginx/sites-available/my-server and create a symlink to it in /etc/nginx/sites-enabled . There is a default config, which you could use as a reference.

After that you could use command sudo /usr/sbin/nginx -t to check configuration. If everything is OK use /etc/init.d/nginx reload to apply new configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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