Move URL with redirects in Nginx

October 3, 2016 74 views
Nginx Node.js

I have a node app running on http://localhost:3000. I set up ssl/https with LetsEncrypt so all requests are 301 redirected to https. Im using Nginx as web server.

I'm trying to move my URL from to

  1. How do I forward requests to AND to my node app?
  2. If users go to AND they should automatically be redirected to

How do I do this?

1 Answer

You can forward your request to Node.js app by using proxy_pass directive.
Look at your server block.
It is usually in /etc/nginx/sites-available/default if you didn't changed default settings. You can make it like this:

server {
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;

    location / {
        return 301$request_uri;    
    location /resume {
        proxy_pass http://localhost:3000/;

This will ensure that your traffic to and will get redirected to Node.js app.
About redirection, just create a server block that will listen on and redirect to

server {
    listen 80;
    listen [::]:80;
    return 301$request_uri;

This will listen only for http traffic. If you used https on, you should still make it listen on 443 with cert, so it doesn't report Server not found error if accessed via https.
This should do its job, I hope it isn't a lot late.

After you done with changes, test your nginx with

  • sudo nginx -t

If everything goes well just restart it:

  • sudo systemctl restart nginx

Try and observe results, if it is not working correctly, we will make it :D
Edit: added more informations.

Have another answer? Share your knowledge.