Score:0

Is there a difference between nginx single or multiple server configuration?

de flag

There are several services running on different ports on my web server. These services are providing data via WebSocket.

Currently, each service has its own server in the nginx configuration, like this:

server {
        listen 9031 ssl;
        location / {
                proxy_pass http://127.0.0.1:9002;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_read_timeout 86400;
        }
}

server {
        listen 8031 ssl;
        location / {
                proxy_pass http://127.0.0.1:8002;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_read_timeout 86400;
        }
}

server {
        listen 7031 ssl;
        location / {
                proxy_pass http://127.0.0.1:7002;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_read_timeout 86400;
        }
}

server {
        listen 6031 ssl;
        location / {
                proxy_pass http://127.0.0.1:6002;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_read_timeout 86400;
        }
}

server {
        listen 5031 ssl;
        location / {
                proxy_pass http://127.0.0.1:5002;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_read_timeout 86400;
        }
}

What I also could do is something like this:

server {
        listen 9031 ssl;
        location /service1 {
                proxy_pass http://127.0.0.1:9002;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_read_timeout 86400;
        }

        location /service2 {
                proxy_pass http://127.0.0.1:8002;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_read_timeout 86400;
        }

        location /service3 {
                proxy_pass http://127.0.0.1:7002;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_read_timeout 86400;
        }

        ...
}

The second approach looks a bit nicer (and a nice side effect would be to have fewer ports that needs to be configured on the router). But my questions are now: is there a method to prefer? Are there any side-effects in terms of performance, stability etc.? What is the recommended approach or is it the same which one to use?

Score:1
us flag

I have used both approaches in the past and both are valid configurations.

In the second approach, keep an eye out for missing paths in the URLs in your response to the client. You may need to use a rewrite or something similar. There are a several approaches to accommodate for this.

For example, if I request https://www.example.com/service1, the response from http://127.0.0.1:9002 needs to include the path /service1 so any subsequent requests will reach the intended service.

Lars avatar
de flag
thx for your response. That helps with decision making.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.