Double Redirect

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 redirects to https://www.example.com

We also get an error:
nginx -t
nginx: [warn] conflicting server name “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 IP Address;
return 301 https://www.example.com$request_uri;
}

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

server {
listen 443 ssl http2;
server_name 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” on 0.0.0.0:80,
ignored

Thank you for your help in advance
Lebod

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

Lebod Wrote:

nginx: [warn] conflicting server name “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 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

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 to https://www.example.com.
Testing out our site, what happens is that http://example.com redirects to
https://example.com which then redirects to https://www.example.com

We also get an error:
nginx -t
nginx: [warn] conflicting server name “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 IP Address;
return 301 https://www.example.com$request_uri;
}

If http://example.com redirects to https://example.com, then that
server{}
is not used for the 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” on 0.0.0.0:80, ignored

nginx -T

might show you where the problem is.

f

Francis D. [email protected]

Lebod Wrote:

Wouldn’t this stop https://example.com from re-directing to
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

Thank you for the reply;

If we did this:

server {
listen 80;
-listen 443 ssl;
server_name example.com IP Address;
return 301 https://www.example.com$request_uri;
}

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

Posted at Nginx Forum:
https://forum.nginx.org/read.php?2,267700,267735#msg-267735

itpp2012 Wrote:

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

Wouldn’t this stop https://example.com from re-directing to
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 to https://www.example.com ?

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

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:
https://forum.nginx.org/read.php?2,267700,267850#msg-267850

Lebod Wrote:

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

Doesn’t that re-direct https://example.com to 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.

https://www.nginx.com/resources/wiki/start/

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

Thank you for the reply Francis,

I have only 1 server {} with “example.com”.

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

server {
listen 80;
listen 443 ssl;
server_name example.com IP Address;
return 301 https://www.example.com$request_uri;
}

Is that not correct?

Thank you v much

Posted at Nginx Forum:
https://forum.nginx.org/read.php?2,267700,267874#msg-267874

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.

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. [email protected]

Hi,

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

listen 443 ssl;
server_name 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.

Echo Arief,

Thanks for the reply.
My complete set up is below. The only difference is the use of
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 IP Address;
return 301 https://www.example.com$request_uri;
}

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

server {
listen 443 ssl http2;
server_name 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

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 to 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:
https://forum.nginx.org/read.php?2,267700,267881#msg-267881

Hi,

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

server_name 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs