# Double Redirect

**URL:** <https://www.ruby-forum.com/t/double-redirect/244814>\
**Category:** NGINX\
**Created:** [June 19, 2016, 11:13pm UTC](https://www.ruby-forum.com/t/double-redirect/244814 "2016-06-19T23:13:10Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [June 19, 2016, 11:13pm UTC](https://www.ruby-forum.com/t/double-redirect/244814/1 "2016-06-19T23:13:10Z")

</div>

Hi everybody,

I’m having an issue with our site. We’re trying to redirect all traffic  
of  
[http://example.com](http://example.com) to [https://www.example.com](https://www.example.com).  
Testing out our site, what happens is that [http://example.com](http://example.com) redirects  
to  
[https://example.com](https://example.com) which then redirects to [https://www.example.com](https://www.example.com)

We also get an error:  
nginx -t  
nginx: [warn] conflicting server name “[example.com](http://example.com)” on 0.0.0.0:80,  
ignored

My server block file is below. I should note that the SSL certificate  
parameters are handled globally in the nginx.conf file and not in the  
server  
block files.

server {  
listen 80;  
listen 443 ssl;  
server\_name [example.com](http://example.com) _ **IP Address** _;  
return 301 https://www.example.com$request\_uri;  
}

server {  
listen 80;  
server\_name [www.example.com](http://www.example.com);  
return 301 https://www.example.com$request\_uri;  
}

server {  
listen 443 ssl http2;  
server\_name [www.example.com](http://www.example.com);  
root /home/forge/example.com/public;

```
index index.html index.htm index.php;

charset utf-8;

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/example.com/server/*;

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }

access_log off;
error_log /var/log/nginx/example.com-error.log error;

error_page 404 /index.php;

location ~* ^/ ***A CERTAIN BLOCKED DIRECTORY**** {
    auth_basic "closed website";
    auth_basic_user_file /etc/nginx/htpasswd;
    allow IP1;
    allow IP2;
    allow IP3;
    allow IP4;
    deny all;

    location ~* .(php) {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
   fastcgi_index index.php;
    include fastcgi_params;
    }
    try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|png|gif|ico|css|js|svg|woff)$ {
   expires 1M;
    add_header Cache-Control "public";
}

# CSS and Javascript
location ~* \.(?:css|js)$ {
    expires 1M;
    access_log off;
    add_header Cache-Control "public";
}

# cache.appcache, your document html and data
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
    expires -1;
    # access_log logs/static.log; # I don't usually include a static

```

log  
}

```
location ~ /\.ht {
    deny all;
}

if ($allowed_country = no) {
    return 444;
}

```

}

I was wondering if there was an issue in our setup that’s causing the  
double  
redirect and the error:  
nginx: [warn] conflicting server name “[example.com](http://example.com)” on 0.0.0.0:80,  
ignored

Thank you for your help in advance  
Lebod

Posted at Nginx Forum:

> **[Double Redirect](https://forum.nginx.org/read.php?2,267700,267700#msg-267700)**
>
> Hi everybody, I'm having an issue with our site. We're trying to redirect all traffic of http://example.com to https://www.example.com. Testing out our site, what happens is that http://example.com redirects to https://example.com which then...

---

<div class="post-metadata">

**Author:** ![Lebod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/54ee81/32.png) [@Lebod](https://www.ruby-forum.com/u/Lebod)\
**Post date:** [June 19, 2016, 11:17pm UTC](https://www.ruby-forum.com/t/double-redirect/244814/2 "2016-06-19T23:17:51Z")

</div>

## Lebod Wrote:

> nginx: [warn] conflicting server name “[example.com](http://example.com)” on 0.0.0.0:80,

It says it all.

> server {  
> listen 80;  
> remove this line: \> listen 443 ssl;  
> server\_name [example.com](http://example.com) _ **IP Address** _;  
> return 301 https://www.example.com$request\_uri;  
> }

Posted at Nginx Forum:  
[https://forum.nginx.org/read.php?2,267700,267701#msg-267701](https://forum.nginx.org/read.php?2,267700,267701#msg-267701)

---

<div class="post-metadata">

**Author:** ![Lebod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/54ee81/32.png) [@Lebod](https://www.ruby-forum.com/u/Lebod)\
**Post date:** [June 20, 2016, 1:15am UTC](https://www.ruby-forum.com/t/double-redirect/244814/3 "2016-06-20T01:15:16Z")

</div>

On Sun, Jun 19, 2016 at 05:12:27PM -0400, Lebod wrote:

Hi there,

> I’m having an issue with our site. We’re trying to redirect all traffic of  
> [http://example.com](http://example.com) to [https://www.example.com](https://www.example.com).  
> Testing out our site, what happens is that [http://example.com](http://example.com) redirects to  
> [https://example.com](https://example.com) which then redirects to [https://www.example.com](https://www.example.com)
> 
> We also get an error:  
> nginx -t  
> nginx: [warn] conflicting server name “[example.com](http://example.com)” on 0.0.0.0:80, ignored

That suggests that there is another server{} block in, or included in,  
your nginx.conf that you have not shown here.

> server {  
> listen 80;  
> listen 443 ssl;  
> server\_name [example.com](http://example.com) _ **IP Address** _;  
> return 301 https://www.example.com$request\_uri;  
> }

If [http://example.com](http://example.com) redirects to [https://example.com](https://example.com), then that  
server{}  
is not used for the [http://example.com](http://example.com) request.

> I was wondering if there was an issue in our setup that’s causing the double  
> redirect and the error:  
> nginx: [warn] conflicting server name “[example.com](http://example.com)” on 0.0.0.0:80, ignored

nginx -T

might show you where the problem is.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Lebod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/54ee81/32.png) [@Lebod](https://www.ruby-forum.com/u/Lebod)\
**Post date:** [June 20, 2016, 11:52pm UTC](https://www.ruby-forum.com/t/double-redirect/244814/4 "2016-06-20T23:52:17Z")

</div>

## Lebod Wrote:

> Wouldn’t this stop [https://example.com](https://example.com) from re-directing to  
> [https://www.example.com](https://www.example.com) then ?

What do you think this does?: return 301  
https://www.example.com$request\_uri;

Posted at Nginx Forum:  
[https://forum.nginx.org/read.php?2,267700,267736#msg-267736](https://forum.nginx.org/read.php?2,267700,267736#msg-267736)

---

<div class="post-metadata">

**Author:** ![Lebod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/54ee81/32.png) [@Lebod](https://www.ruby-forum.com/u/Lebod)\
**Post date:** [June 20, 2016, 11:05pm UTC](https://www.ruby-forum.com/t/double-redirect/244814/5 "2016-06-20T23:05:33Z")

</div>

Thank you for the reply;

If we did this:

server {  
listen 80;  
-listen 443 ssl;  
server\_name [example.com](http://example.com) _ **IP Address** _;  
return 301 https://www.example.com$request\_uri;  
}

Wouldn’t this stop [https://example.com](https://example.com) from re-directing to  
[https://www.example.com](https://www.example.com) then ?

Posted at Nginx Forum:

> **[Re: Double Redirect](https://forum.nginx.org/read.php?2,267700,267735#msg-267735)**
>
> Hi everybody, I'm having an issue with our site. We're trying to redirect all traffic of http://example.com to https://www.example.com. Testing out our site, what happens is that http://example.com redirects to https://example.com which then...

---

<div class="post-metadata">

**Author:** ![Lebod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/54ee81/32.png) [@Lebod](https://www.ruby-forum.com/u/Lebod)\
**Post date:** [June 20, 2016, 11:59pm UTC](https://www.ruby-forum.com/t/double-redirect/244814/6 "2016-06-20T23:59:53Z")

</div>

## itpp2012 Wrote:

> > return 301 https://www.example.com$request\_uri;  
> > }
> > 
> > Wouldn’t this stop [https://example.com](https://example.com) from re-directing to  
> > [https://www.example.com](https://www.example.com) then ?
> 
> What do you think this does?: return 301  
> https://www.example.com$request\_uri;

Doesn’t that re-direct [https://example.com](https://example.com) to [https://www.example.com](https://www.example.com) ?

Posted at Nginx Forum:  
[https://forum.nginx.org/read.php?2,267700,267738#msg-267738](https://forum.nginx.org/read.php?2,267700,267738#msg-267738)

---

<div class="post-metadata">

**Author:** ![Lebod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/54ee81/32.png) [@Lebod](https://www.ruby-forum.com/u/Lebod)\
**Post date:** [June 24, 2016, 6:45am UTC](https://www.ruby-forum.com/t/double-redirect/244814/7 "2016-06-24T06:45:47Z")

</div>

Thanks for the tip Francis but Nginx -T doesn’t give me a clue at all.  
Would you know what specifically I can change to fix this?

Thank you so much

Posted at Nginx Forum:

> **[Re: Double Redirect](https://forum.nginx.org/read.php?2,267700,267850#msg-267850)**
>
> Hi everybody, I'm having an issue with our site. We're trying to redirect all traffic of http://example.com to https://www.example.com. Testing out our site, what happens is that http://example.com redirects to https://example.com which then...

---

<div class="post-metadata">

**Author:** ![Lebod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/54ee81/32.png) [@Lebod](https://www.ruby-forum.com/u/Lebod)\
**Post date:** [June 21, 2016, 12:25am UTC](https://www.ruby-forum.com/t/double-redirect/244814/8 "2016-06-21T00:25:00Z")

</div>

Lebod Wrote:

> > What do you think this does?: return 301  
> > https://www.example.com$request\_uri;
> 
> Doesn’t that re-direct [https://example.com](https://example.com) to [https://www.example.com](https://www.example.com)  
> ?

It would if you used “listen 443”, maybe you should read the docs to get  
a  
basic grasp what your doing.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

Posted at Nginx Forum:  
[https://forum.nginx.org/read.php?2,267700,267739#msg-267739](https://forum.nginx.org/read.php?2,267700,267739#msg-267739)

---

<div class="post-metadata">

**Author:** ![Lebod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/54ee81/32.png) [@Lebod](https://www.ruby-forum.com/u/Lebod)\
**Post date:** [June 25, 2016, 7:43am UTC](https://www.ruby-forum.com/t/double-redirect/244814/9 "2016-06-25T07:43:34Z")

</div>

Thank you for the reply Francis,

I have only 1 server {} with “[example.com](http://example.com)”.

I have one server block that listens on ports 80 and 443 to [example.com](http://example.com)  
and  
redirects to [https://www.example.com](https://www.example.com) like this:

server {  
listen 80;  
listen 443 ssl;  
server\_name [example.com](http://example.com) _ **IP Address** _;  
return 301 https://www.example.com$request\_uri;  
}

Is that not correct?

Thank you v much

Posted at Nginx Forum:

> **[Re: Double Redirect](https://forum.nginx.org/read.php?2,267700,267874#msg-267874)**
>
> Hi everybody, I'm having an issue with our site. We're trying to redirect all traffic of http://example.com to https://www.example.com. Testing out our site, what happens is that http://example.com redirects to https://example.com which then...

---

<div class="post-metadata">

**Author:** ![Lebod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/54ee81/32.png) [@Lebod](https://www.ruby-forum.com/u/Lebod)\
**Post date:** [June 25, 2016, 2:31am UTC](https://www.ruby-forum.com/t/double-redirect/244814/10 "2016-06-25T02:31:02Z")

</div>

On Fri, Jun 24, 2016 at 12:45:00AM -0400, Lebod wrote:

Hi there,

> Thanks for the tip Francis but Nginx -T doesn’t give me a clue at all.  
> Would you know what specifically I can change to fix this?

You have two server{} blocks that have server\_name [example.com](http://example.com).

That is a problem.

Decide what config you want, and make that happen. Perhaps the fix is  
to completely remove the first such block. Perhaps it is something else.

Cheers,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Lebod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/54ee81/32.png) [@Lebod](https://www.ruby-forum.com/u/Lebod)\
**Post date:** [June 25, 2016, 7:50am UTC](https://www.ruby-forum.com/t/double-redirect/244814/11 "2016-06-25T07:50:34Z")

</div>

Hi,

On Sat, Jun 25, 2016, at 14:42, Lebod wrote:

> listen 443 ssl;  
> server\_name [example.com](http://example.com) _ **IP Address** _;  
> return 301 https://www.example.com$request\_uri;  
> }
> 
> Is that not correct?

Looking at your previous posts indicated there are more server blocks  
than the ones you posted. Posting output of nginx -T may help.

---

<div class="post-metadata">

**Author:** ![Lebod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/54ee81/32.png) [@Lebod](https://www.ruby-forum.com/u/Lebod)\
**Post date:** [June 25, 2016, 11:09pm UTC](https://www.ruby-forum.com/t/double-redirect/244814/12 "2016-06-25T23:09:27Z")

</div>

Echo Arief,

Thanks for the reply.  
My complete set up is below. The only difference is the use of  
“[example.com](http://example.com)”  
instead of the domain for privacy reasons.I was really hoping that a  
gracious person would just point out the mistake in the set up if there  
is  
one…

server {  
listen 80;  
listen 443 ssl;  
server\_name [example.com](http://example.com) _ **IP Address** _;  
return 301 https://www.example.com$request\_uri;  
}

server {  
listen 80;  
server\_name [www.example.com](http://www.example.com);  
return 301 https://www.example.com$request\_uri;  
}

server {  
listen 443 ssl http2;  
server\_name [www.example.com](http://www.example.com);  
root /home/forge/example.com/public;

index index.html index.htm index.php;

charset utf-8;

# FORGE CONFIG (DOT NOT REMOVE!)

include forge-conf/example.com/server/\*;

location / {  
try\_files $uri $uri/ /index.php?$query\_string;  
}

location = /favicon.ico { access\_log off; log\_not\_found off; }  
location = /robots.txt { access\_log off; log\_not\_found off; }

access\_log off;  
error\_log /var/log/nginx/example.com-error.log error;

error\_page 404 /index.php;

location ~\* ^/_ **A CERTAIN BLOCKED DIRECTORY** _\* {  
auth\_basic “closed website”;  
auth\_basic\_user\_file /etc/nginx/htpasswd;  
allow IP1;  
allow IP2;  
allow IP3;  
allow IP4;  
deny all;

location ~\* .(php) {  
fastcgi\_split\_path\_info ^(.+.php)(/.+)$;  
fastcgi\_pass unix:/var/run/php/php7.0-fpm.sock;  
fastcgi\_index index.php;  
include fastcgi\_params;  
}  
try\_files $uri $uri/ /index.php?$query\_string;  
}

location ~ .php$ {  
fastcgi\_split\_path\_info ^(.+.php)(/.+)$;  
fastcgi\_pass unix:/var/run/php/php7.0-fpm.sock;  
fastcgi\_index index.php;  
include fastcgi\_params;  
}

# Media: images, icons, video, audio, HTC

location ~\* .(?:jpg|jpeg|png|gif|ico|css|js|svg|woff)$ {  
expires 1M;  
add\_header Cache-Control “public”;  
}

# CSS and Javascript

location ~\* .(?:css|js)$ {  
expires 1M;  
access\_log off;  
add\_header Cache-Control “public”;  
}

# cache.appcache, your document html and data

location ~\* .(?:manifest|appcache|html?|xml|json)$ {  
expires -1;

# access\_log logs/static.log; # I don’t usually include a static log

}

location ~ /.ht {  
deny all;  
}

if ($allowed\_country = no) {  
return 444;  
}  
}

Posted at Nginx Forum:  
[https://forum.nginx.org/read.php?2,267700,267877#msg-267877](https://forum.nginx.org/read.php?2,267700,267877#msg-267877)

---

<div class="post-metadata">

**Author:** ![Lebod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/54ee81/32.png) [@Lebod](https://www.ruby-forum.com/u/Lebod)\
**Post date:** [June 26, 2016, 6:48pm UTC](https://www.ruby-forum.com/t/double-redirect/244814/13 "2016-06-26T18:48:17Z")

</div>

I’ve found the problem. Thank you everybody for the tip about nginx -T

The web app that I use “forge” to deploy servers seemed to add another  
redirect hidden inside of /etc/nginx/forge-conf/before/ssl\_redirect.conf  
which was redirecting port 80 [example.com](http://example.com) to [https://example.com](https://example.com)

My server block was correct, it was just this file that was adding an  
additional redirect.

Thanks again

Posted at Nginx Forum:

> **[Re: Double Redirect](https://forum.nginx.org/read.php?2,267700,267881#msg-267881)**
>
> Hi everybody, I'm having an issue with our site. We're trying to redirect all traffic of http://example.com to https://www.example.com. Testing out our site, what happens is that http://example.com redirects to https://example.com which then...

---

<div class="post-metadata">

**Author:** ![Lebod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/54ee81/32.png) [@Lebod](https://www.ruby-forum.com/u/Lebod)\
**Post date:** [June 26, 2016, 4:48am UTC](https://www.ruby-forum.com/t/double-redirect/244814/14 "2016-06-26T04:48:51Z")

</div>

Hi,

On Sun, Jun 26, 2016, at 06:09, Lebod wrote:

> server\_name [example.com](http://example.com) _ **IP Address** _;  
> listen 443 ssl http2;  
> location / {
> 
> fastcgi\_split\_path\_info ^(.+.php)(/.+)$;  
> fastcgi\_index index.php;  
> location ~\* .(?:css|js)$ {
> 
> location ~ /.ht {  
> deny all;  
> }
> 
> if ($allowed\_country = no) {  
> return 444;  
> }  
> }

That doesn’t seem to be the output of `nginx -T`.
