skip to Main Content

I have 2 domains, let’s call them mydomain1.com and mydomain2.com

I also have a VPS, and I’m trying to host both of them on same VPS however without mydomain1.com:port

Also, I use pterodactyl panel to host them if that helps
Code: (same for both, just second one has different domain)

server {
    listen 20002;
    server_name mydomain1.com;

    #access_log /home/container/naccess.log;
    #error_log  /home/container/nerror.log error;

    root /home/container/webroot;
    index index.html index.htm index.php;
    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    # allow larger file uploads and longer script runtimes
    client_max_body_size 100m;
    client_body_timeout 120s;
    sendfile off;
    location ~ .php$ {
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass unix:/home/container/tmp/php-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param PHP_VALUE "upload_max_filesize = 100M n post_max_size=100M";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTP_PROXY "";
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
    }

    location ~ /.ht {
        deny all;
    }
}

I tried to change first server_name to mydomain1.com and second server_name to mydomain2.com, but I can only access site using mydomain1.com:port or mydomain2.com:port since they aren’t hosted on port 80.

2

Answers


  1. Here is what an NGINX config would look like if you wanted to use a different backend for each domain name. Adjust as needed.

    This config listens for traffic on on port 80 and 443. Upstream ports can be the same or different btw.

    If domain one.com is accessed from port 80, it will serve the contents of 1.1.1.1:2000

    If domain two.com is accessed from port 443, it will serve the contents of 2.2.2.2:2001

    http {
        upstream one {
            server 1.1.1.1:2000;
        }
    
        upstream two {
            server 2.2.2.2:2001;
        }
    
        server {
            listen 80;
            server_name one.com;
    
            location / {
                proxy_pass http://one;
            }
        }
    
        server {
            listen 443 ssl;
            server_name two.com;
    
            ssl_certificate /path/to/certificate.pem;
            ssl_certificate_key /path/to/private.key;
    
            location / {
                proxy_pass https://two;
            }
        }
    }
    

    If you wanted to listen on a single port like port 4000, and direct traffic to backends appropriately, then this would be an example config:

    http {
        upstream one {
            server 1.1.1.1:2000;
        }
    
        upstream two {
            server 2.2.2.2:2001;
        }
    
        server {
            listen 4000;
            server_name one.com two.com;
    
            location / {
                if ($http_host = "one.com") {
                    proxy_pass http://one;
                }
                if ($http_host = "two.com") {
                    proxy_pass http://two;
                }
            }
        }
    }
    
    Login or Signup to reply.
  2. If you do not already have Nginx installed on your VPS, install it from the Linux distribution’s package manager.

    Change the nginx.conf file: Go to the Nginx root folder and locate the nginx.conf file. Ensure the conf file contains:
       include /etc/nginx/conf.d/*.conf;
    

    to look into file configurations on a specific location in the startup. There you can create configuration files to host websites.

    You can create server blocks to host multiple domains, such as abc.com and abc1.com
    Create abc.com.conf in the NGINX root folder /etc/nginx/conf.d/ to hold the configuration for abc.com

    server {
        listen 80 default_server;
        listen [::]:80 default_server;  
        root /var/www/abc.com;  
        index index.html;  
        server_name abc.com www.abc.com;    
        location / { try_files $uri $uri/ =404;}
    }
    

    Create abc1.com.conf in the NGINX root folder /etc/nginx/conf.d/. to hold the configuration for abc1.com

    server {
        listen 80;
        listen [::]:80;  
        root /var/www/abc1.com;  
        index index.html;  
        server_name abc1.com abc1.com;      
        location / { try_files $uri $uri/ =404;}
    }
    

    Create folders to host website files:

    Create two directories, abc.com and abc1.com, in /var/www/.
    Upload website files to the host
    Now place your static file content so that server can find them. The next step is to navigate to /var/www/abc.com and /var/www/abc1.com and place the site’s HTML and static files there.

    To apply the new configuration, reboot the webserver using
    sudo systemctl restart nginx

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search