Question

How to setup nginx with subdomain only

Hey all,

All of this is very new to me, I’m a junior frontend developer trying to learn more about well everything… and I’ve gotten a bit stuck with nginx as I’ve never used it before.

I have been following tutorials to setup Strapi CMS in a DO droplet and got to the stage of setting up nginx using this tutorial: https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04

I’ve completed everything upto and including step 4, but step 5 is where I am lost.

When I go to my ip, lets say 1.2.3.4 I get the nginx page, when I go to 1.2.3.4:1337 I get the strapi page and when I go to 1.2.3.4:1337/admin/ I get the log in page.

I have my domain in cloudflare, and have made a sub-domain admin.mydomain.com which takes me to nginx homepage, but admin.mydomain.com:1337/admin and admin.mydomain.com/admin do nothing. What I am wanting to do, is have admin.mydomain.com take me to 1.2.3.4:1337/admin/.

However, I don’t understand the next steps and have searched the questions in here but not found something that helps me. Sorry if this has been asked and I just haven’t been able to find it.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
May 28, 2021
Accepted Answer

Hi @yedroj92,

You’ll need to configure Nginx as a reverse proxy for your domain. Don’t worry though, it’s not that hard.

In your Nginx configuration file for your subdomain - admin.mydomain.com , you can add the following location block:

    location / {
        proxy_pass http://your_server_ip:1337;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /admin {
        proxy_pass http://your_server_ip:1337/admin;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

Make sure to add the location block inside your other block, so it will look something like:

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

        root /var/www/admin.mydomain.com/html;

        server_name admin.mydomain.com www.admin.mydomain.com;

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

        location / {
           proxy_pass http://your_server_ip:1337;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
       }

       location /admin {
           proxy_pass http://your_server_ip:1337/admin;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
       }
}

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel