Score:0

How to add multiple ip and add sub-domain in nginx?

ru flag

I was following the nginx,uwsgi with flask & docker tutorial. My nginx config file contains:

server {

listen 5000;
server_name 19X.X.X.X;

client_header_buffer_size 5M;
large_client_header_buffers 4 5M;
client_max_body_size 75M;

location / {
    include uwsgi_params;     
    uwsgi_pass flask:8080;
   }
}

Here I want to add an additional IP address like 123.345.67.8 and a subdomain test.mydomain.com. How can I add there?

EDIT 2:


server {

listen 1.1.1.1:5000 default_server;
server_name localhost;

client_header_buffer_size 5M;
large_client_header_buffers 4 5M;
client_max_body_size 75M;

location / {
    include uwsgi_params;     
    uwsgi_pass flask:8080;
   }
}


server {

listen 2.2.2.2:5000 default_server;
server_name test.mydomain.com;

client_header_buffer_size 5M;
large_client_header_buffers 4 5M;
client_max_body_size 75M;

location / {
    include uwsgi_params;     
    uwsgi_pass flask:8080;
   }
}
Ivan Shatsky avatar
gr flag
You can list any number of server names with the `server_name` directive. However having only one server block like this, nginx will use it to process every request coming to the TCP port 5000 no matter what IP address it come to or what is the HTTP `Host` header value. See the [How nginx processes a request](http://nginx.org/en/docs/http/request_processing.html) official documentation page or [this](https://stackoverflow.com/a/60362700/7121513) answer for even more details.
ru flag
@IvanShatsky Thanks for your reply. Please see the EDIT1 of my question. If I do so then can I access it with "1.1.1.1:5000" as well as "test.mydomain.com" ?
Ivan Shatsky avatar
gr flag
I think you didn't got an idea about the **default server**. If your server listens on both `1.1.1.1` and `2.2.2.2` IP addresses, your first server block will server every request coming to the port 5000. You don't need to define the second server block. You'll need it only if you want to serve different sites on `1.1.1.1` and `2.2.2.2`. And using `test.mydomain.com` in the browser adddress bar will made the browser to use 80/443 port depending on `htttp://` or `https://` scheme, you'll need to explicitly specify the port: `http://test.mydomain.com:5000/...`
ru flag
@IvanShatsky That's a clear idea about the default server. Thanks. But let me clarify one issue. For example, I want to add 1.1.1.1 IP which works for private network access. And want to add 2.2.2.2 -> https://test.mydomain.com for accessing it publicly. Then how can I modify for this issue?
ru flag
Please see the EDIT 2 (updated) is it going to work now ?
Ivan Shatsky avatar
gr flag
From my understanding, if you want to serve the same site for both addresses, you don't need anything other than a single server block with the `listen 5000;` directive. You don't need to even specify a server name if your `test.mydomain.com` domain resolves to your address. It will serve any request coming to the TCP port 5000. I think the links I provide earlier are quite explainable.
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.