✓ Solved

How do I redirect from root domain to subdomain?

I'm using nginx and running a webapp at myserver.com/app. I would like to redirect myserver.com to myserver.com/app. My webapp is deployed via a container (dokku) and I don't know if this affects the required solution.

This is my first time using nginx and linode, so I'm not very familiar with how this works. My previous experience has been working a bit with apache and shared hosting where my files had a folder "public_html" where I could put a .htaccess file.

3 Replies

✓ Best Answer

I don’t believe the above config would perform any redirects - it just allows myserver.com to return the content in /var/www/myserver.com/app.

You need another server block that “listens” on myserver.com and www.myserver.com and returns a redirect to app.myserver.com. (The afore-mentioned block could be used for app.myserver.com by changing the server_name directive accordingly.

Here is a sample config from my own website that redirects from www.andysh.uk to andysh.uk (using HTTPS) - as an example for you to work with. The _common and _common-ssl files just include some additional directives I use across all sites, like error pages and SSL ciphers.

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name www.andysh.uk;

  ssl_certificate /srv/www-ssl/andysh.uk/fullchain.pem;
  ssl_certificate_key /srv/www-ssl/andysh.uk/key.pem;

  include conf.d/_common;
  include conf.d/_common-ssl;

  return 301 https://andysh.uk$request_uri;
}

This is a pretty default NGINX config as an example.

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

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

    server_name _;

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

To do what you want, you need to set the path in root and your domain in server_name

server_name myserver.com;

root /var/www/myserver.com/app;

Then restart your container.

Thank you. I found the file that has this. Would

server_name myserver.com;

root /var/www/myserver.com/app;

redirect both www.myserver.com and myserver.com to app.myserver.com?

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct