How to redirect domain to www url with Nginx


Dev-Bookmarks Logo

Save up to a workweek a year by efficiently managing your dev bookmarks, on www.bookmarks.dev. Share your favorites with the community and they will be published on Github - 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 dev bookmarks in february 2019

Lots of exciting #DevBookmarks in february 2019. Keywords: api, architecture, arquillian, aws, bash, bootstrap, chai, cheatsheet, devops, diagram, dns, documentation, domain-driven-design, error-handling, facebook, favicon, ffmpeg, free-programming-books, functional-programming, gif, git, graph, html, html5, integration-testing, java, javascript, junit, jvm, kotlin, latex, linux, markdown, microservices, middleware, mocha, mongodb, monitoring, nginx, nodejs, oidc, online-tools, open-source, openid-connect, osx, pandoc, podcast, promise, reactive, reactive-programming, reactjs, redis, rest, rxjs, shell, spring, spring-boot, spring-cloud-gateway, spring-security, spring-webflux, sql, testing, tools, video-processing, visualization, web, websocket, windows and zuul Continue reading