How to redirect domain to www url with Nginx


Dev-Bookmarks Logo

(P) Bookmarks.dev - Open source Bookmarks and Codelets Manager for Developers & Co. See our How To guides to help you get started. Share your favorites bookmarks with the community and they might get published on Github - Star


This post presents the snippet from the Nginx configuration that redirects all request to https://www.bookmarks.dev:

# redirect HTTP to www
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name codingmarks.org www.codingmarks.org;

    return 301 https://www.codingmarks.org$request_uri;
}

# redirect HTTPS to wwww
server {
    listen 443;
    server_name codingmarks.org;

    return 301 https://www.codingmarks.org$request_uri;
}

Note the missing www.codingmarks.org in the second server/server_name entry, to avoid an infinite loop

Lots of other good Nginx resources can be found if you search for the nginx tag on codingmarks: https://www.bookmarks.dev?q=[nginx]

Octocat Source code for bookmarks.dev is available on Github.

Subscribe to our newsletter for more code resources and news

Adrian Matei

Adrian Matei
Life force expressing itself as a coding capable human being

My troubles getting to run a Node application with PM2 in cluster mode with NVM

In this blog post I will list of the trouble I went through getting to run PM2 in cluster mode on an Ubuntu system, on which I had Node.js both standalone installed and managed with NVM. Continue reading