By TheBlackBenzKid


2011-10-30 18:56:01 8 Comments

I am using nginx on Rackspace cloud following a tutorial and having searched the net and so far can't get this sorted.

I want www.mysite.com to go to mysite.com as normal in .htaccess for SEO and other reasons.

My /etc/nginx/sites-available/www.example.com.vhost config:

server {
       listen 80;
       server_name www.example.com example.com;
       root /var/www/www.example.com/web;

       if ($http_host != "www.example.com") {
                 rewrite ^ http://example.com$request_uri permanent;
       }

I have also tried

server {
       listen 80;
       server_name example.com;
       root /var/www/www.example.com/web;

       if ($http_host != "www.example.com") {
                 rewrite ^ http://example.com$request_uri permanent;
       }

I also tried. Both the second attempts give redirect loop errors.

if ($host = 'www.example.com' ) {
rewrite ^ http://example.com$uri permanent;
}

My DNS is setup as standard:

site.com 192.192.6.8 A type at 300 seconds
www.site.com 192.192.6.8 A type at 300 seconds

(example IPs and folders have been used for examples and to help people in future). I use Ubuntu 11.

17 comments

@Drakes 2019-02-25 01:39:10

If you don't want to hardcode the domain name, you can use this redirect block. The domain without the leading www is saved as variable $domain which can be reused in the redirect statement.

server {
    ...
    # Redirect www to non-www
    if ( $host ~ ^www\.(?<domain>.+) ) {
       rewrite ^/(.*)$ $scheme://$domain/$1;
    }
}

REF: Redirecting a subdomain with a regular expression in nginx

@cnst 2019-01-23 00:37:54

  1. Best Practice: separate server w/ hardcoded server_name

Best practice with nginx is to use a separate server for a redirect like this (not shared with the server of your main configuration), to hardcode everything, and not use regular expressions at all.

It may also be necessary to hardcode the domains if you're using HTTPS, because you have to know upfront which certificates you'll be providing.

server {
    server_name www.example.com;
    return  301 $scheme://example.com$request_uri;
}
server {
    server_name www.example.org;
    return  301 $scheme://example.org$request_uri;
}
server {
    server_name example.com example.org;
    # real configuration goes here
}

  1. Using Regular Expressions within server_name

If you have a number of sites, and don't care for the most ultimate performance, but want every single one of them to have the same policy in regards to the www. prefix, then you can use regular expressions. The best practice of using a separate server would still stand.

Note that this solution gets tricky if you use https, as you must then have a single certificate to cover all of your domain names if you want this to work properly.


non-www to www w/ regex in a dedicated single server for all sites:

server {
    server_name ~^(?!www\.)(?<domain>.+)$;
    return  301 $scheme://www.$domain$request_uri;
}

www to non-www w/ regex in a dedicated single server for all sites:

server {
    server_name ~^www\.(?<domain>.+)$;
    return  301 $scheme://$domain$request_uri;
}

www to non-www w/ regex in a dedicated server for some sites only:

It may be necessary to restrict the regex to cover only a couple of domains, then you can use something like this to only match www.example.org, www.example.com and www.subdomain.example.net:

server {
    server_name ~^www\.(?<domain>(?:example\.org|example\.com|subdomain\.example\.net))$;
    return  301 $scheme://$domain$request_uri;
}

Testing Regular Expressions w/ nginx

You can test that the regex works as expected with pcretest on your system, which is the exact same pcre library that your nginx will be using for regular expressions:

% pcretest 
PCRE version 8.35 2014-04-04

  re> #^www\.(?<domain>(?:example\.org|example\.com|subdomain\.example\.net))$#
data> test
No match
data> www.example.org
 0: www.example.org
 1: example.org
data> www.test.example.org
No match
data> www.example.com
 0: www.example.com
 1: example.com
data> www.subdomain.example.net
 0: www.subdomain.example.net
 1: subdomain.example.net
data> subdomain.example.net
No match
data> www.subdomain.example.net.
No match
data> 

Note that you don't have to worry about trailing dots or case, as nginx already takes care of it, as per nginx server name regex when "Host" header has a trailing dot.


  1. Sprinkle if within existing server / HTTPS:

This final solution is generally not considered to be the best practice, however, it still works and does the job.

In fact, if you're using HTTPS, then this final solution may end up easier to maintain, as you wouldn't have to copy-paste a whole bunch of ssl directives between the different server definitions, and could instead place the snippets only into the needed servers, making it easier to debug and maintain your sites.


non-www to www:

if ($host ~ ^(?!www\.)(?<domain>.+)$) {
    return  301 $scheme://www.$domain$request_uri;
}

www to non-www:

if ($host ~ ^www\.(?<domain>.+)$) {
    return  301 $scheme://$domain$request_uri;
}

hardcoding a single preferred domain

If you want a little bit more performance, as well as consistency between multiple domains a single server may use, it might still make sense to explicitly hardcode a single preferred domain:

if ($host != "example.com") {
    return  301 $scheme://example.com$request_uri;
}

References:

@FRECIA 2019-07-30 02:40:08

Worked perfecly thank you cnst. I had no need to repeat the "litsen 80" line as shown in other answers, so it seems redundant.

@Red 2016-08-28 13:35:51

You need two server blocks.

Put these into your config file eg /etc/nginx/sites-available/sitename

Let's say you decide to have http://example.com as the main address to use.

Your config file should look like this:

server {
        listen 80;
        listen [::]:80;
        server_name www.example.com;
        return 301 $scheme://example.com$request_uri;
}
server {
        listen 80;
        listen [::]:80;
        server_name example.com;

        # this is the main server block
        # insert ALL other config or settings in this server block
}

The first server block will hold the instructions to redirect any requests with the 'www' prefix. It listens to requests for the URL with 'www' prefix and redirects.

It does nothing else.

The second server block will hold your main address — the URL you want to use. All other settings go here like root, index, location, etc. Check the default file for these other settings you can include in the server block.

The server needs two DNS A records.

Name: @ IPAddress: your-ip-address (for the example.com URL)

Name: www IPAddress: your-ip-address (for the www.example.com URL)

For ipv6 create the pair of AAAA records using your-ipv6-address.

@Matt Janssen 2018-04-25 02:56:30

I combined the best of all the simple answers, without hard-coded domains.

301 permanent redirect from non-www to www (HTTP or HTTPS):

server {
    if ($host !~ ^www\.) {
        rewrite ^ $scheme://www.$host$request_uri permanent;
    }

    # Regular location configs...
}

If you prefer non-HTTPS, non-www to HTTPS, www redirect at the same time:

server {
    listen 80;

    if ($host !~ ^www\.) {
        rewrite ^ https://www.$host$request_uri permanent;
    }

    rewrite ^ https://$host$request_uri permanent;
}

@TheBlackBenzKid 2018-04-25 05:46:01

Great answer. I heard that using Else was "evil" though ?

@Kevin Nguyen 2013-10-08 17:18:22

try this

    if ($host !~* ^www\.){
        rewrite ^(.*)$ https://www.yoursite.com$1;
    }

Other way: Nginx no-www to www

server {
  listen       80;
  server_name  yoursite.com;
  root /path/;
  index index.php;
  return       301 https://www.yoursite.com$request_uri;
}

and www to no-www

server {
  listen       80;
  server_name  www.yoursite.com;
  root /path/;
  index index.php;
  return       301 https://yoursite.com$request_uri;
}

@Drew 2014-06-29 01:11:13

Generally best to avoid If's as outlined: wiki.nginx.org/IfIsEvil

@Greg Smethells 2015-03-30 18:06:34

Why did the authors provide an if-statement in nginx and then tell people to avoid it? Sounds flippant to me.

@Kukunin 2015-04-01 19:56:37

There is stated "IF in location is evil". You can safely put if into your server block

@Justin E 2015-10-02 20:27:05

Direct quote from the above link...The only 100% safe things which may be done inside if in location context are: return ...; rewrite ... last;

@Ravindra Bhalothia 2017-07-25 07:19:11

Redirect non-www to www

For Single Domain :

server {
        server_name example.com;
        return 301 $scheme://www.example.com$request_uri;
}

For All Domains :

server {
        server_name "~^(?!www\.).*" ;
        return 301 $scheme://www.$host$request_uri;
}

Redirect www to non-www For Single Domain:

server {
        server_name www.example.com;
        return 301 $scheme://example.com$request_uri;
}

For All Domains :

server {
         server_name "~^www\.(.*)$" ;
         return 301 $scheme://$1$request_uri ;
}

@Hassan Baig 2017-07-25 20:34:05

Could you provide differentiation between 80 and 443?

@Metagrapher 2018-01-23 07:41:01

These will not work without listen directives

@Ibrahim 2018-08-10 18:56:05

It seems to work without listen directives for me (nginx 1.4.6).

@karadayi 2016-07-10 21:36:31

if ($host ~* ^www.example.com$) {
    return 301 $scheme://example.com$request_uri;
}

@Maoz Zadok 2016-05-23 17:09:50

location / { 
    if ($http_host !~ "^www.domain.com"){ 
        rewrite ^(.*)$ $scheme://www.domain.com/$1 redirect; 
    } 
}

@karimhossenbux 2017-06-26 09:00:30

$scheme://www.domain.com$1 to avoid double slash

@Martin Höger 2015-05-14 14:46:18

You may find out you want to use the same config for more domains.

Following snippet removes www before any domain:

if ($host ~* ^www\.(.*)$) {
    rewrite / $scheme://$1 permanent;
}

@ck_ 2015-08-25 15:09:47

I like this way better than dedicated server blocks. Change http to $scheme

@MrYellow 2015-10-27 20:17:24

Much better, can't believe so many would hardcode domains into configs for this task.

@Adam 2016-07-27 09:53:34

@Oli That link does not (as of today) mention performance but rather that they're not 100% safe. It does say "The only 100% safe things which may be done inside if in a location context are: return ... and rewrite ... last". Any updated links to performance issues?

@pierreb 2016-09-23 16:05:36

Indeed, rewrite... last and return seem to be safe on server level. Here's from the Nginx website: In some cases, it’s also possible to move ifs to server level (where it’s safe as only other rewrite module directives are allowed within it). No reference to having an impact on performance.

@Nico Brenner 2018-02-14 07:48:31

This didn't work for me. Kept getting an error on the browser saying invalid response.

@pozitron57 2018-06-18 19:44:07

Official Nginx documentation states that If is evil.

@Martin Höger 2018-07-10 13:34:30

Unfortunately, I didn't find a way without "if". I use the same config for many domains, hardcoding the domain names is not an option. Any suggestion/comment is appreciated!

@VisioN 2015-10-16 15:44:33

This solution comes from my personal experience. We used several Amazon S3 buckets and one server for redirecting non-www to www domain names to match S3 "Host" header policy.

I used the following configuration for nginx server:

server {
    listen 80;
    server_name ~^(?!www\.)(?<domain>.+)$;
    return 301 $scheme://www.$domain$request_uri;
}

This matches all domain names pointed to the server starting with whatever but www. and redirects to www.<domain>. In the same manner you can do opposite redirect from www to non-www.

@Toskan 2017-08-10 17:04:22

what about https? note: https NEED the certificate

@VisioN 2017-08-11 08:05:02

There is absolutely no problem with HTTPS here. After listen 80 you need to add listen 443 ssl and then ssl_certificate and ssl_certificate_key directives.

@Toskan 2017-08-11 19:44:52

nobody uses http nowadays. I was reading a top listed guide in google that showed your example just with the added line listen 443 ssl with missing certficate. That wont work and is causing some serious headache.

@VisioN 2017-08-11 20:06:50

I don't know what guide you are talking about. I have this configuration successfully working for nearly three years. Last year I added support for SSL and it works as expected. And of course you need to have a certificate with a private key in hand.

@Metagrapher 2018-01-23 07:43:37

so this will clobber all subdomains except www, correct?

@Fleshgrinder 2012-07-31 04:58:55

Actually you don't even need a rewrite.

server {
    #listen 80 is default
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

server {
    #listen 80 is default
    server_name example.com;
    ## here goes the rest of your conf...
}

As my answer is getting more and more up votes but the above as well. You should never use a rewrite in this context. Why? Because nginx has to process and start a search. If you use return (which should be available in any nginx version) it directly stops execution. This is preferred in any context.

Redirect both, non-SSL and SSL to their non-www counterpart:

server {
    listen               80;
    listen               443 ssl;
    server_name          www.example.com;
    ssl_certificate      path/to/cert;
    ssl_certificate_key  path/to/key;

    return 301 $scheme://example.com$request_uri;
}

server {
    listen               80;
    listen               443 ssl;
    server_name          example.com;
    ssl_certificate      path/to/cert;
    ssl_certificate_key  path/to/key;

    # rest goes here...
}

The $scheme variable will only contain http if your server is only listening on port 80 (default) and the listen option does not contain the ssl keyword. Not using the variable will not gain you any performance.

Note that you need even more server blocks if you use HSTS, because the HSTS headers should not be sent over non-encrypted connections. Hence, you need unencrypted server blocks with redirects and encrypted server blocks with redirects and HSTS headers.

Redirect everything to SSL (personal config on UNIX with IPv4, IPv6, SPDY, ...):

#
# Redirect all www to non-www
#
server {
    server_name          www.example.com;
    ssl_certificate      ssl/example.com/crt;
    ssl_certificate_key  ssl/example.com/key;
    listen               *:80;
    listen               *:443 ssl spdy;
    listen               [::]:80 ipv6only=on;
    listen               [::]:443 ssl spdy ipv6only=on;

    return 301 https://example.com$request_uri;
}

#
# Redirect all non-encrypted to encrypted
#
server {
    server_name          example.com;
    listen               *:80;
    listen               [::]:80;

    return 301 https://example.com$request_uri;
}

#
# There we go!
#
server {
    server_name          example.com;
    ssl_certificate      ssl/example.com/crt;
    ssl_certificate_key  ssl/example.com/key;
    listen               *:443 ssl spdy;
    listen               [::]:443 ssl spdy;

    # rest goes here...
}

I guess you can imagine other compounds with this pattern now by yourself.

More of my configs? Go here and here.

@TheBlackBenzKid 2012-08-22 14:57:33

Thanks for this. Repped.

@Chuan Ma 2013-03-25 10:24:05

Is there a need for the $scheme variable?

@Fleshgrinder 2013-03-30 01:16:22

What do you want to do? Normally you use an absolute URL but to be honest I never tried a protocol relative URL in the location block (e.g. //example.com$request_uri). Of course you can use a absolute URL without any host (e.g. /new_uri) but that doesn't make sense in this context.

@chuck reynolds 2014-08-09 02:15:24

@Fleshgrinder I used your setup and keep getting problems with www won't work... keep getting a HSTS failure in chrome. Any idea on that?

@Fleshgrinder 2014-08-09 06:26:37

Your Chrome shouldn't be able to go to the www domain of yours if you're using HSTS. Please open a new question with as many details as possible and I'll help you (you can post the URL to the question as a comment here).

@YPCrumble 2015-04-04 21:08:13

@Fleshgrinder I'm trying to implement your setup but getting the following issue at stackoverflow.com/questions/29451409/… Any ideas on how to make it work?

@Jeff Tsay 2015-08-20 20:37:26

In the 2nd block "Redirect both, non-SSL and SSL to their non-www counterpart:", both server blocks should have the SSL directives, as the browser needs to verify the cert for www.example.com before it redirects to example.com.

@Fleshgrinder 2015-08-22 12:39:46

Of course, I added that as well as a short info about HSTS.

@n611x007 2015-10-27 09:18:25

so what if you have only 1 cert? for no-www. will example.com still redirect to example.com . suppose not, why (guess nginx serves only with correct cert?) should there be an error log about this (failed connection attempt to non-served ... well, I can see a problem here...) in nginx somewhere?

@Fleshgrinder 2015-10-27 18:21:57

A www.example.com certificate is always valid for example.com as well. No problem there. Of course such a non-wildcard certificate is not valid for e.g. www2.example.com and you would need a different certificate. Nginx does not log an error in such a case if you either have not configured that domain or if you have configured it with a self-signed certificate. Simply because the validation fails on the client side and not on the server side. Hope this answers your question, otherwise create a real one.

@jacurtis 2016-10-10 06:51:17

This is the preffered method for this type of redirect. It is truly a redirect unlike the selected answer above.

@Fleshgrinder 2016-10-10 12:50:15

return 301 is a true 301 HTTP redirect with the location and everything set.

@Maciej Krawczyk 2017-06-01 17:13:43

This is the best answer by far

@YPCrumble 2018-01-02 20:38:44

@Fleshgrinder is there a reason you use multiple server blocks rather than one server block listening at both 80 and 443, with example.com and www.example.com as server_name directives, and then include redirects like if ($host ~* ^www\.(.*)) { return 301 https://$1$request_uri; } if ($scheme != "https") { return 301 https://$host$request_uri; } (sorry if poorly formatted)?

@Fleshgrinder 2018-01-09 18:43:23

@YPCrumble yes, it's MUCH faster this way because we are not performing regular expression matching on each request. We only redirect if we know that we have to redirect. No checks, no validation, nothing: just redirect. =)

@Ahmed Essam 2018-01-10 11:41:53

I found that after changing specific configurations in nginx, nginx -s reload doesn't seem to be enough. Restarting the nginx service was the only the solution for me to make things work.

@Khom Nazid 2019-03-08 14:01:09

Thank you for the 443 clarity too. This should be the correct answer <3

@Andriyun 2015-02-19 16:01:47

Unique format:

server {
  listen 80;
  server_name "~^www\.(.*)$" ;
  return 301 https://$1$request_uri ;
}

@Ramast 2015-07-29 10:36:29

You can make it generic by writing it this way: server { server_name "~^www\.(.*)$" ; return 301 $scheme://$1$request_uri ; }

@Eric Johnson 2014-12-10 17:00:29

Here's how to do it for multiple www to no-www server names (I used this for subdomains):

server {
        server_name 
             "~^www\.(sub1.example.com)$"
             "~^www\.(sub2.example.com)$"
             "~^www\.(sub3.example.com)$";
         return 301 $scheme://$1$request_uri ;
}

@TheBlackBenzKid 2014-12-11 11:02:28

This is very smart; plus + 1 for this.

@Nico Brenner 2018-02-14 07:47:33

Great solution, worked perfectly! Thank you :)

@Abhishek Goel 2018-08-30 04:40:59

Great :) This is what I was looking for :)

@nottinhill 2014-11-15 15:58:51

Ghost blog

in order to make nginx recommended method with return 301 $scheme://example.com$request_uri; work with Ghost you will need to add in your main server block:

proxy_set_header    X-Real-IP           $remote_addr;
proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
proxy_set_header    Host                $http_host;
proxy_set_header    X-Forwarded-Proto   $scheme;
proxy_set_header    X-NginX-Proxy       true;

proxy_pass_header   X-CSRF-TOKEN;
proxy_buffering     off;
proxy_redirect      off;  

@TheBlackBenzKid 2011-10-31 19:18:07

HTTP Solution

From the documentation, "the right way is to define a separate server for example.org":

server {
    listen       80;
    server_name  example.com;
    return       301 http://www.example.com$request_uri;
}

server {
    listen       80;
    server_name  www.example.com;
    ...
}

HTTPS Solution

For those who want a solution including https://...

server {
        listen 80;
        server_name www.domain.com;
        # $scheme will get the http protocol
        # and 301 is best practice for tablet, phone, desktop and seo
        return 301 $scheme://domain.com$request_uri;
}

server {
        listen 80;
        server_name domain.com;
        # here goes the rest of your config file
        # example 
        location / {

            rewrite ^/cp/login?$ /cp/login.php last;
            # etc etc...

        }
}

Note: I have not originally included https:// in my solution since we use loadbalancers and our https:// server is a high-traffic SSL payment server: we do not mix https:// and http://.


To check the nginx version, use nginx -v.

Strip www from url with nginx redirect

server {
    server_name  www.domain.com;
    rewrite ^(.*) http://domain.com$1 permanent;
}

server {
    server_name  domain.com;
    #The rest of your configuration goes here#
}

So you need to have TWO server codes.

Add the www to the url with nginx redirect

If what you need is the opposite, to redirect from domain.com to www.domain.com, you can use this:

server {
    server_name  domain.com;
    rewrite ^(.*) http://www.domain.com$1 permanent;
}

server {
    server_name  www.domain.com;
    #The rest of your configuration goes here#
}

As you can imagine, this is just the opposite and works the same way the first example. This way, you don't get SEO marks down, as it is complete perm redirect and move. The no WWW is forced and the directory shown!

Some of my code shown below for a better view:

server {
    server_name  www.google.com;
    rewrite ^(.*) http://google.com$1 permanent;
}
server {
       listen 80;
       server_name google.com;
       index index.php index.html;
       ####
       # now pull the site from one directory #
       root /var/www/www.google.com/web;
       # done #
       location = /favicon.ico {
                log_not_found off;
                access_log off;
       }
}

@puk 2011-11-30 10:38:14

+1 For the level of detail

@TheBlackBenzKid 2011-11-30 18:11:13

@puk appreciate it. Nginx is amazing, but good documentation that stays up to date with server version and OS and server hardware changes is quite tiresome. The best resource that serves me is howtoforge.com as it supports the RackSpace cloud versions. Some of the commands above won't work in later versions. But this nginx/0.8.54 - is believe me, the best nginx server) no need to upgrade or update. Works fine. 100,000 unique hits a day with 4200 transactions average a day. Nginx is RAPID. like using a site with no traffic.

@Roberto 2012-10-10 20:08:17

Your rewrites should become returns, as in return 301 $scheme://domain.com$request_uri;. There is no need to capture any patterns, see Nginx pitfalls

@Art 2013-04-19 02:27:36

your solution will only work for http, not https. See what other people have suggested here re "return 301 $scheme"

@TheBlackBenzKid 2013-04-19 08:25:40

@Art I have cleaned and updated my solution.

@tomis 2013-05-14 21:02:51

@TheBlackBenzKid Sorry, maybe I have missed something, but updated solution is not working. It's because listen 80 - with that, you are saying that only HTTP is matching this. There should be more ports to listen if same configuration is used for HTTP and HTTPS... Or? But definitelly helped me, +1. Thanks for reply. Cheers.

@TheBlackBenzKid 2013-05-15 07:50:36

@tomis It is an example really and the solution works with latest nginx.. better to post another question for specifics. Thanks

@tomis 2013-05-15 11:33:23

@TheBlackBenzKid It was just note. I have found out working solution. In your example, only Listen 443 should be added and complete working.

@detj 2013-10-24 21:59:18

Awesome! Thanks

@pcarvalho 2014-03-20 18:43:29

"To check the nginx version, use nginx -v. Here is a solution for older nginx versions..." older than what?

@TheBlackBenzKid 2014-03-21 09:41:36

@peteroak does not matter as that command works through out wiki.nginx.org/CommandLine

@romanlv 2014-05-26 16:29:25

solution work, but there is only a problem with comments, nginx does not recognize '//' for comments, '#' should be used instead

@Rami GB 2014-06-03 19:09:52

I've been doing it for a while i was falling in a redirect loop, i just noticed that there are two server blocks now and it worked, thank you.

@Paul 2014-06-26 18:33:01

I had to fix a bunch of these in site-based NGINX config files, so wrote a little python script to do it, posted at: gist.github.com/DrPaulBrewer/61b6ba76de31df22722c

@parisssss 2014-10-02 17:18:57

@TheBlackBenzKid is there any different in redirecting subdomains? for example www.blog.google.com to blog.google.com

@sospedra 2014-11-12 14:53:44

Best nginx answer ever, gz and ty

@nottinhill 2014-11-15 15:39:52

No this not correct answer. See below.

@user636044 2014-12-28 05:52:22

Don't you need listen 443 ssl for HTTPS to work?

@r3wt 2015-12-22 21:10:23

answer is wrong. it redirects all subdomains to www.

@Nathan Hornby 2016-01-28 15:12:32

I'm not following why there are apparently two approaches here, out of the blue it says "To check the nginx version, use nginx -v." and then goes onto an alternative approach. Is this supposed to be for a specific version of nginx? Am I just missing something?

@TheBlackBenzKid 2016-01-31 10:36:04

@NathanHornby There is one solution. It works for http and https. NGINX has also configuration power for loadbalancers and using it as a proxy. This page has a wealth of information for you to get setup. The nginx -v is only to check the version, it was just a reference for future. This should work through out all versions. Thanks

@Ostad 2016-04-01 12:28:44

Is it possible to make it generic? I mean make it work with every domain instead of www.domain.com (or in your example www.google.com)

@steven 2014-06-04 01:33:15

not sure if anyone notice it may be correct to return a 301 but browsers choke on it to doing

rewrite ^(.*)$ https://yoursite.com$1; 

is faster than:

return 301 $scheme://yoursite.com$request_uri;

@steven 2017-04-04 20:54:24

my comment was directed to the browser not to efficiency on nginx side! with a redirect the browser makes 2 requests vs 1 request when rewiting

@undoIT 2013-10-28 19:14:25

If you are having trouble getting this working, you may need to add the IP address of your server. For example:

server {
listen XXX.XXX.XXX.XXX:80;
listen XXX.XXX.XXX.XXX:443 ssl;
ssl_certificate /var/www/example.com/web/ssl/example.com.crt;
ssl_certificate_key /var/www/example.com/web/ssl/example.com.key;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}

where XXX.XXX.XXX.XXX is the IP address (obviously).

Note: ssl crt and key location must be defined to properly redirect https requests

Don't forget to restart nginx after making the changes:

service nginx restart

@TheBlackBenzKid 2013-10-29 15:03:29

/etc/init.d/nginx reload you can reload the server too which does not cause any downtime.

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] Node.js + Nginx - What now?

  • 2011-02-15 20:49:02
  • Van Coding
  • 331703 View
  • 941 Score
  • 11 Answer
  • Tags:   node.js nginx concept

1 Answered Questions

[SOLVED] Strange Nginx behavior with trailing slashes

1 Answered Questions

[SOLVED] How to perform multiple redirects in NGINX

  • 2018-07-06 03:28:50
  • HelloWorld
  • 590 View
  • 0 Score
  • 1 Answer
  • Tags:   nginx webserver

2 Answered Questions

[SOLVED] Nginx - No WWW in header

  • 2018-04-16 13:56:15
  • user500963
  • 31 View
  • 0 Score
  • 2 Answer
  • Tags:   nginx

0 Answered Questions

Trying to get www.example.com from a subfolder /example.com into a NGINX server of mine

  • 2017-10-05 11:44:58
  • PatrizioRD
  • 44 View
  • 0 Score
  • 0 Answer
  • Tags:   php nginx hosts

2 Answered Questions

[SOLVED] Nginx non-www not working and redirect in Ubuntu 16.04

  • 2016-12-31 11:33:47
  • aldesabido
  • 963 View
  • 0 Score
  • 2 Answer
  • Tags:   ubuntu nginx

1 Answered Questions

Can multi laravel based sites be accessed by folder not by port?

1 Answered Questions

Folder wise Nginx Configuration

1 Answered Questions

[SOLVED] Vagrant / puppet config for complex vhost setting (if statements etc.)?

1 Answered Questions

[SOLVED] How to setup nginx to allow access with /suburi on ubuntu 12.04?

Sponsored Content