DB-hub Technology NGINX How to Redirect Non WWW to WWW in Nginx?

How to Redirect Non WWW to WWW in Nginx?

erver {
    server_name  example.com;
    return       301 http://www.example.com$request_uri;
}
server {
        listen 80 default_server;
        root /usr/share/nginx/html;
        index index.html index.htm;
        server_name www.example.com;
}

The first server block shown above will serve all requests that hits the server with the name of “example.com”. It will then return a 301 HTTP status code with the location of “http://www.example.com” followed by rest of the URL that the end user typed.

Once the end user (browser or any http client) gets that 301 response, it will again send the request to the location suggested ( that is http://www.example.com). This request will be served by the second server block which will respond back with the actual data(that is the HTML page). Let’s see this practically using curl command(curl is a command line tool to send requests to web servers).

Note the fact that the lines beginning with > indicates the headers of the HTTP request being sent. Lines beginning with < indicates HTTP response being returned by the server(which clearly shows 301 status, with the value of “http://www.example.com” in the Location field. Browsers and other clients obey this response, and will then send another request to the “Location”. This is how 301 redirection works).

Important: Please note the fact that DNS entries should be there for example.com and www.example.com (pointing to the same IP) for this to work.

How to Redirect HTTP to HTTPS in Nginx?

Now lets see how to solve our second problem. This is to enforce https for all the users visiting your website. Basically any request in the form of http://www.example.com should be redirected to https://www.example.com

Let’s see an example nginx configuration snippet which achieves this.

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://server_namerequest_uri;
}
server {
        listen 443 default_server ssl;
        server_name example.com www.example.com;
        ssl_certificate    /etc/nginx/server.crt;
        ssl_certificate_key    /etc/nginx/server.key;
        root /usr/share/nginx/html;
        index index.html index.htm;
}

The only thing worth noticing is the use of HTTPS in the URL after 301. server_name indicates the website name defined in both the server blocks. Remember to include server_name in your configuration for this to work. If there is no server_name set, then it wont work. This is because server_name variable will be blank without setting it, and it will be a redirection to https:///.

Also remember to have the second server section with appropriate SSL certificate in the correct path.

There is another method to do this HTTP to HTTPS redirection in nginx(shown below)

server {
    listen 80;
    server_name example.com www.example.com;
    rewrite     ^   https://server_namerequest_uri? permanent;
}
server {
        listen 443 default_server ssl;
        server_name example.com www.example.com;
        ssl_certificate    /etc/nginx/server.crt;
        ssl_certificate_key    /etc/nginx/server.key;
        root /usr/share/nginx/html;
        index index.html index.htm;
}

Leave a Reply

您的邮箱地址不会被公开。 必填项已用 * 标注