huzeyfe
huzeyfe

Reputation: 3704

nginx: Configuring multiple vhost without a subdomain

I have an nginx server. I have a public IP address but I don't have any subdomains. I would like to split my dev and test servers. So can I set up these servers ie

http://55.22.11.127/dev and http://55.22.11.127/test

I've tried this in config:

server_name 55.22.11.127/dev;

No luck...

So do I have to set up a sub domain or can I set up this using trailing url it dev and test?

Upvotes: 0

Views: 151

Answers (1)

Mark Stosberg
Mark Stosberg

Reputation: 13381

server_name must be a domain name or an IP address.

There is a kind of workaround if either dev or test is only to be accessed from your laptop.

You can add the laptop-only domain name to your laptop's /etc/hosts file and map it to that server name. For that matter, you can put both subdomains in there if you want.

55.22.11.127 test.example.com dev.example.com

Then in Nginx you can set server_name to match the values in your /etc/hosts` files, with different server blocks for the two domains.

server {
    server_name test.example.com;
    ...
}
server {
    server_name dev.example.com;
    ...
}

To support multiple hosts, your collaborators either need to also share the same /etc/hosts entries, or you need to use real DNS entries.

This trick is also great for testing production DNS values before they resolve on the public internet.

A final option to use one server block for both cases, but use two different location blocks to contain the configurations for each of the two cases. For example, you can set different root directive values in two different locations.

Upvotes: 1

Related Questions