I have taken the task to try and figure out how to use Nginx to host my websites (previously using IIS). I purchased a VPS running CentOS 8. I have been trying to configure Nginx to run two different domains on the same port.

I have examined and followed multiple guides online and I am still unable to target both websites.

After installing Nginx and configuring the service, I created two conf files in /etc/nginx/conf.d directory:


server {
    listen 80;
    root /var/www/website1.com;
    index index.html index.htm;
    server_name website1.com www.website1.com;

    location / {
            try_files $uri $uri/ =404;



server {
    listen 80;
    root /var/www/website2.com;
    index index.html index.htm;
    server_name website2.com www.website2.com;

    location / {
            try_files $uri $uri/ =404;


I then also created two basic index.html files in the directory /var/www/website(1|2).com

Initially I had to remove the default server block from the nginx.conf file to get it to load anything other that the default site.


# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;


I have been trying to load the website using this URL with my IP address and also localhost ( It is my understanding Nginx matches incoming requests first against port then by server name, both of my config files are on listening on port 80.

I would really appreciate some direction on what I am doing wrong here as everything I have seems to follow documentation.


You need to use the hostname (e.g. website2.com) in the URL. That hostname should be in DNS or, for testing, in /etc/hosts or equivalent for your workstation OS.

Edit: Here is how the web browser is working. When you have an URI (URL), the string is split in multiple components (protocol, user, password, host, path, query, fragment...)

name resolution - Host is the one we are interested. It can be a hostname or an IP. If it is a hostname, the name will be resolved by the resolver library located on the web client machine. This library by default will use hosts file, then DNS to resolve the name.

virtual host aka nginx Server Blocks - the web browser will make a HTTP request that will contain Host: header to tell the web server which hostname he is trying to access. That is why you need to have a hostname and not an IP if you want to test those servers.

  • Thanks for the comment, so I believe this will allow me to test the nginx config within the VPS. One thing I am however wondering is how could I access the website externally from my own machines over the web? I might be misunderstanding how nginx works but I believed if I type http:\\x.x.x.x/ServerName, it should have been able to resolve it – beaver559 Jun 21 '20 at 18:56
  • To make it work from outside for anyone, you must use the DNS hostname and the server needs to be added to a public DNS server. For testing only you can add an "ip hostname" entry into /etc/hosts or c:\Windows\System32\Drivers\etc\hosts, but this will work only on the machines where you have edited that file. – Mircea Vutcovici Jun 21 '20 at 19:41
  • I have two domains that I own, I can update the A records to point at the IP address of my webserver. How would can I manage how each domain points to each website in nginx respectively? This is the crux of what I am trying to research – beaver559 Jun 21 '20 at 20:00
  • I updated my answer – Mircea Vutcovici Jun 22 '20 at 15:26
  • Thank you very much for the detail in the answers, I managed to get it working by just pointing my domains directly at the webserver and nginx was able to handle it already. I think I had the misconception that I could just call the webserver with a URI that would load each website respectively. – beaver559 Jun 22 '20 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.