• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here: Home » Nginx WWW to No WWW & HTTP to HTTPS

By Abhishek Ghosh June 17, 2015 5:01 am Updated on June 17, 2015

Nginx WWW to No WWW & HTTP to HTTPS

Advertisement

Basic, Yet Many New Users Fight With Nginx WWW to No WWW & HTTP to HTTPS Rules on Cloud Server Instance Running Debian/Ubuntu GNU/Linux. They have rightly configured the infrastructure to run common web applications like WordPress on HP Cloud. Basically it is not exactly difficult for an old user but can give a pain to a new user who was using Apache2 for many years. We have full Nginx-PHP5 FPM configuration as well in this guide. These are cloud server specific and agonistic for future rapid change in server.

 

Nginx WWW to No WWW & HTTP to HTTPS

 

For practical example, think about the domain thecustomizewindows.com. If we had no HSTS, then the first condition for pointing the non-SSL domain http://thecustomizewindows.com from http://www.thecustomizewindows.com is :

Single Server, Non-SSL :

Advertisement

---

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
    server_name  www.thecustomizewindows.com;
    rewrite ^(.*) http://thecustomizewindows.com$1 permanent;
}
server {
       listen 80 default;
       server_name thecustomizewindows.com;
       index index.php index.html;
       root /usr/share/nginx/html;
       location = /favicon.ico {
                log_not_found off;
                access_log off;
# ...
# rest config
# ...
 
       }
}

rewrite is faster than return :

Vim
1
return 301 $scheme://thecustomizewindows.com$request_uri;

The server block for 443 remaining commented out. Do not read rest of the guide, it is for more complicated situation. Know quite well, if you add the IP against the A Name for both non-WWW and WWW from DNS settings in HP Cloud, Rackspace, IBM etc. with higher TTL, the root server automatically redirects before reaching the request towards the server.

Single Server, SSL :

In this case, first we will redirect the www and then non-www towards the https site :

Vim
1
2
3
4
5
6
7
8
9
10
11
server {
    server_name  www.thecustomizewindows.com;
    rewrite ^(.*) https://thecustomizewindows.com$1 permanent;
}
server {
       listen 80;
       server_name thecustomizewindows.com;
       rewrite ^(.*) https://thecustomizewindows.com$1 permanent;
       index index.php index.html;
       root /usr/share/nginx/html;
}

In this case, the HTTPS server block of the above will bear default in the listen part :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
       listen 443 default;
       server_name thecustomizewindows.com;
       index index.php index.html;
       root /usr/share/nginx/html;
       location = /favicon.ico {
                log_not_found off;
                access_log off;
# ...
# rest config
# ...
 
       }
}

www for HTTPS location will also need redirection, so the config for 443 port :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
server {
       listen 443;
       server_name www.thecustomizewindows.com;
       rewrite ^(.*) https://thecustomizewindows.com$1 permanent;
       index index.php index.html;
       root /usr/share/nginx/html;
       location = /favicon.ico {
                log_not_found off;
                access_log off;
# ...
# ssl configuration required for HSTS
# ...
 
       }
 
}
server {
       listen 443 default;
       server_name thecustomizewindows.com;
       index index.php index.html;
       root /usr/share/nginx/html;
       location = /favicon.ico {
                log_not_found off;
                access_log off;
# ...
# rest config
# ssl config
# ...
 
       }
}

Ultimately we have 4 virtual hosts from permutation and combination :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
server {
    listen 80;
    server_name  www.thecustomizewindows.com;
    rewrite ^(.*) https://thecustomizewindows.com$1 permanent;
}
server {
       listen 80;
       server_name thecustomizewindows.com;
       rewrite ^(.*) https://thecustomizewindows.com$1 permanent;
}
server {
       listen 443;
       server_name www.thecustomizewindows.com;
       rewrite ^(.*) https://thecustomizewindows.com$1 permanent;
       index index.php index.html;
       root /usr/share/nginx/html;
       location = /favicon.ico {
                log_not_found off;
                access_log off;
# ...
# ssl configuration required for HSTS
# ...
 
       }
 
}
server {
       listen 443 default;
       server_name thecustomizewindows.com;
       index index.php index.html;
       root /usr/share/nginx/html;
       location = /favicon.ico {
                log_not_found off;
                access_log off;
# ...
# rest config
# ssl configuration
# ...
 
       }
}

It looks too crude, but it is great for the new users. All virtual hosts will redirect to :

Vim
1
2
3
4
http://www.thecustomizewindows.com -> https://thecustomizewindows.com
http://thecustomizewindows.com -> https://thecustomizewindows.com
https://www.thecustomizewindows.com -> https://thecustomizewindows.com
https://thecustomizewindows.com -> 200 OK (root /usr/share/nginx/html)

Now a more complicated situation like ours where www subdomain is on different server :

Multiple Server with separate WWW server, SSL :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
    listen 80;
    server_name  www.thecustomizewindows.com;
    rewrite ^(.*) http://thecustomizewindows.com$1 permanent;
}
server {
       listen 443;
       server_name www.thecustomizewindows.com;
       rewrite ^(.*) https://thecustomizewindows.com$1 permanent;
       index index.php index.html;
       root /usr/share/nginx/html;
       location = /favicon.ico {
                log_not_found off;
                access_log off;
# ...
# ssl configuration required for HSTS
# ...
 
       }
 
}

Nginx WWW to No WWW & HTTP to HTTPS

Yes, our method is bad to look at for single server SSL, but it returns the 301 faster and easy to troubleshoot. You can do curl for each to see the header :

Vim
1
2
3
4
curl -I http://thecustomizewindows.com
curl -I http://www.thecustomizewindows.com
curl -I https://www.thecustomizewindows.com
curl -I http://thecustomizewindows.com

We are focussed on HSTS. Exactly like said for plain case; if you add the IP against the A Name for both non-WWW and WWW from DNS settings in HP Cloud, Rackspace, IBM etc. with higher TTL, the root server automatically redirects before reaching the request towards the server except that for HSTS. That is determined by preload time in HSTS. If you see our WordPress Nginx Configuration file on GitHub, you will see :

Vim
1
2
3
4
        return 301 https://thecustomizewindows.com$request_uri;
if ($http_x_forwarded_proto = "http") {
                rewrite  ^/(.*)$  https://thecustomizewindows.com/$1 permanent;
                }

it is for Rackspace load balancer. Our website had lot of older posts (near 4.5K written over 4 years) on non-SSL. Before we migrated to HP Cloud, we were on Rackspace. We kept it as fail-safe method. If data center needs movement, we can adjust easily. Now think about our complicated thecustomizewindows.net domain with HSTS configuration. All 4 are 301 :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
server {
    listen 80;
    server_name  www.thecustomizewindows.net;
    rewrite ^(.*) https://thecustomizewindows.com$1 permanent;
}
server {
       listen 80;
       server_name thecustomizewindows.net;
       rewrite ^(.*) https://thecustomizewindows.com$1 permanent;
}
server {
       listen 443;
       server_name www.thecustomizewindows.net;
       rewrite ^(.*) https://thecustomizewindows.com$1 permanent;
       index index.php index.html;
       root /usr/share/nginx/html;
       location = /favicon.ico {
                log_not_found off;
                access_log off;
# ...
# ssl configuration required for HSTS
# ...
 
       }
 
}
server {
       listen 443 default;
       server_name thecustomizewindows.net;
       rewrite ^(.*) https://thecustomizewindows.com$1 permanent;
       index index.php index.html;
       root /usr/share/nginx/html;
       location = /favicon.ico {
                log_not_found off;
                access_log off;
# ...
# rest config
# ssl configuration
# ...
 
       }
}

Always test with nginx -t command before running service nginx reload command. Never reboot the server instance without testing with nginx -t after change. When you’ll manage your own server, you’ll delicately manage, when others will manage; unfortunately you might not like all the things.

If all the virtual hosts have own server instances and IP (which is common in high traffic website), these type of configuration helps to easily change the rules by another person. We are not resolving www.thecustomizewindows.com and any thecustomizewindows.net on main server to avoid DDoS. Script Kiddies can run exploit on unmonitored locations and protocols. It is better to keep root /usr/share/nginx/another-location :

Vim
1
2
3
4
5
6
7
server {
    listen 80;
    server_name  www.thecustomizewindows.net;
    rewrite ^(.*) https://thecustomizewindows.com$1 permanent;
       index index.php index.html;
       root /usr/share/nginx/another-location;
}

In case of suspected exploit, you can uncomment the rewrite ^(.*) https://thecustomizewindows.com$1 permanent; and enable log to catch the exploit. You’ll not find these things on StackOverflow. It is not possible to explain so much there. Now if you monitor the files with OSSEC like tools, it becomes very easy to catch the exploit before any thing happens by only one sysadmin. If somehow the hacker gets entry to location /usr/share/nginx/another-location on different server with fake files you can trap it and get the details on their machine. If the server is hacked, redirection is possible from DNS.

You can adapt to easier config, but these are safer for plan to use multiple web servers.

Tagged With https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1826 S7GpOQ1xuSjcx9IZqnTqkGMjB5mPILTQLXL6dPtPuZCtBl4e_HQGMN__xeeGQ0qN 0266160751915474559c261b0bc0f326a809f719&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme , nginx www to no-www , nginx www tono www

This Article Has Been Shared 591 Times!

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Nginx WWW to No WWW & HTTP to HTTPS

  • HHVM WordPress (Nginx Ubuntu Server) Tweaks

    Here are some special HHVM Wordpress (Nginx Ubuntu Server) Tweaks for Page Speed optimization, Compatibility of WordPress Themes and Plugins.

  • Steps To Install Nginx Plus on Ubuntu Server (HP Cloud)

    Here Are the Steps To Install Nginx Plus on Ubuntu Server Running on HP Cloud. Nginx Plus is the Paid Version of Nginx with Extra Features.

  • Zend Opcache Optimization for Nginx Ubuntu (HP Cloud)

    Here is Full Guide to Zend Opcache Optimization for Nginx PHP5-FPM for Ubuntu Server Running on HP Cloud. We Suggest to Use Zend Opcache over APC.

  • Enable Nginx PHP-FPM Status Page (Ubuntu, HP Cloud)

    Here is How To Enable Nginx PHP-FPM Status Page on Ubuntu Server Instance Running on HP Cloud. Enabling Ping Has Difference With Enabling Status.

  • Port knocking in Ubuntu : Hide SSH Daemon on HP Cloud

    Port knocking is used to stop port scan by the attackers who seeks the vulnerable services to attack. Here is guide for the HP Helion Public Cloud Users.

Additionally, performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Exploring the Benefits and Advantages of Microsoft’s Operating System March 22, 2023
  • Web Design Cookbook: Accessibility March 21, 2023
  • Online Dating: How to Find Your Match March 20, 2023
  • Web Design Cookbook: Logo March 19, 2023
  • How Starlink Internet Works March 17, 2023

About This Article

Cite this article as: Abhishek Ghosh, "Nginx WWW to No WWW & HTTP to HTTPS," in The Customize Windows, June 17, 2015, March 23, 2023, https://thecustomizewindows.com/2015/06/nginx-www-to-no-www-http-to-https/.

Source:The Customize Windows, JiMA.in

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT