How to redirect domain to www url with Nginx


Octocat **Promotion** - Efficiently manage your coding bookmarks, aka #codingmarks, on www.codingmarks.org and share your hidden gems with the world. They are published weekly on Github. You can help us build THE programming-resources location - Star


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

# 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.codingmarks.org?q=[nginx]

Octocat Source code for codingmarks.org is available on Github - frontend and backend.

Adrian Matei

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

New codingmarks published in week 45of 2018

New codingmarks published in week 45 of 2018. Keywords: agile, api, docker, docker-compose, expressjs, extreme-programming, free-programming-books, git, java, microservices, mocking, mongodb, nodejs, oauth2, openid-connect, rest, security, spy, ssh, stub, tdd and testing Continue reading

New codingmarks published in week 44 of 2018

Published on November 04, 2018

New codingmarks published in week 39 of 2018

Published on October 04, 2018