1

I'm new to Nginx.

My main goal was to host two Websites for one Domain. I wanted a subdomain hosted separately from the main domain, such as:

  • example.com is served from /home/user/Documents/vue_website/dist
  • subdomain.example.com is served from /var/www/html

Because I run this all on one server I believe this can't be done because they have the same IP address.

I am now trying to get this sorted out by using subdirectories. If i visit example.com/wp it should show the the second site.

Current Nginx site Config looks like this:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    return 301 https://my-domain.com$request_uri;
}

server {
    access_log /var/log/nginx/scripts.log scripts;

    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    ssl_certificate /etc/ssl/my-domain.com.pem;
    ssl_certificate_key /etc/ssl/_.my-domain.com_private_key.key;
    server_name my-domain.com www.my-domain.com;
   location / {

            root /home/pi/Documents/vue_website/dist;
            index index.html;
    }

    location /wp {
            root /var/www/html/;

            location ~ \.php$ {
                 try_files $uri =404;
                 fastcgi_split_path_info ^(.+\.php)(/.+)$;
                 fastcgi_pass 172.0.0.1:9000;
                 fastcgi_index index.php;
                 include fastcgi_params;
            }
    }

}

But that doesn't work. I would appreciate any help.

EDIT: So i got the Subdomain working now. But the problem is now that i can only reach it by ssl / typing https in front which i don't want.

So now i am trying to redirect the http request to the given site based on the domain searched

so for example if i go to http://my-domain.com redirect me to https and so on.

Code i got is as follows:

        'my-domain.com' '1';
        'wp.my-domain.com' '2';
}

server {
        listen 80;

        if ($new = '1') {
                return 301 https://my-domain.com$request_uri;
        }

        if ($new = '2') {
                return 301 https://wp.my-domain.com$request_uri;
        }
}

Do i got something wrong in the Code because in my mind it should work like that if i understood it right.

Subby
  • 13
  • 1
  • 4

1 Answers1

0

Your assumption that you can't host multiple websites on one IP is not correct. You can host as many websites on an IP as you like, either using http or https. You can also host different content using subfolders if you prefer.

Configuring subdomains is fairly trivial, you just use two server blocks

server {
  server_name example.com;
  listen 443 ssl http2;
  // Add other required SSL entries
  root     /var/www/site1;
}

server {
  server_name subdomain.example2.com;
  listen 443 ssl http2;
  // Add other required SSL entries
  root     /var/www/site2;
}

Configuring with subfolders you do this

server {
  server_name example.com;
  listen 443 ssl http2;
  // Add other required SSL entries
  location / {
    root     /var/www/site1;
  }
  location /subdir {
    root     /var/www/site2;
  }
}

If you want further help please edit your question and give us more detail than "it doesn't work". Ideally you would share a curl of a URL, the matching access and error log entry, and useful PHP / application logs, and the expected behavior.

Tim
  • 31,888
  • 7
  • 52
  • 78