Edito
Edito

Reputation: 3747

How to configure nginx's default.conf

I'm trying to configure default.conf in /etc/nginx/conf.d to show a simple landing page located at /home/ubuntu/project-source/company/entry/index.html.

The domains are set up correctly as far as I know to point to the server

A: test24.company.io -> <aws-elastic-IP>

default.conf:

server {
    listen       80;
    server_name  localhost;

    index index.html;

    location / {
        root   /home/ubuntu/project-source/company/entry;
    }
}

server {
    server_name test24.company.io www.test24.company.io;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/test24.company.io/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/test24.company.io/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = test24.company.io) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen      80;
    server_name test24.company.io www.test24.company.io;
    return 404; # managed by Certbot
}

Additional question: the project will run 2 processes on 2 subdomains lets say sub1 and sub2 and they will run on localhost:3001 and localhost:3002 respectively, how do I configure default.conf to point/proxy to these processes as well?

Upvotes: 6

Views: 31701

Answers (2)

Edito
Edito

Reputation: 3747

Ok after many days and hours breaking my head I came to this solution:

  1. On GoDaddy's DNS I added these records:
    A: test24.company.io -> <aws-elastic-IP>
    A: sub1.test24.company.io -> <aws-elastic-IP>
    A: sub2.test24.company.io -> <aws-elastic-IP>
    note: All 3 records point to the same elastic IP, there is no need to set an individual elastic IP for each subdomain.

  2. The nginx conf file is configured as follows: https://gist.github.com/ahmed-abdelazim/b3536c1780afc4215ef57633bbe77a88

This post is a very useful guide on setting nginx proxies for different ports on your server.

All the rest in the nginx config file is generated by certbot, which also managed the redirect from http to https.




note to mods: I cannot seem to format the code of the GitHubGist in this answer for some reason.

Upvotes: 0

Ahmed Abdelazim
Ahmed Abdelazim

Reputation: 741

first use this way to redirect HTTP to HTTPS

server {
listen 80;
listen [::]:80;
server_name example.com www.example.com; 
return 301 https://example.com$request_uri;
}

Second Your server root not included in SSL server it should include

root   /home/ubuntu/project-source/company/entry;

Third You can add what you want of apps on different ports. just copy paste whole server code and change desired values like root location and PORT

after end of edit save and restart server

sudo service nginx restart

Upvotes: 1

Related Questions