We help IT Professionals succeed at work.

no redirect  on nginx

Jasmin shahrzad
Jasmin shahrzad used Ask the Experts™
on
nginx on ubuntu 16.04 not redirect permanently.
i running nginx version 1.12.2
my ssl  page not working and  curl -I  http://webserver.com says
HTTP/1.1 302 MOves Temporrarily

i have the same on other ubuntu exactly same nginx and no problem. what is wrong?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
nociSoftware Engineer
Distinguished Expert 2018

Commented:
curl -L    will follow redirects.   (aka curl --location )

curl -l means for FTP list only
David FavorFractional CTO
Distinguished Expert 2018

Commented:
Also useful...

curl -I -L http://webserver.com

Open in new window


Which will dump all headers + all redirects.

Author

Commented:
doesn't matter. it's not working. i mean when we write http://web-server.com it should redirect to https://....
it doesn't.
curl -I -L http://web-server.com
say :
---------------
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.12.2
Date: Tue, 10 Dec 2019 08:05:06 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: http://aa.com
Strict-Transport-Security: max-age=31536000

HTTP/1.1 302 Found
Server: nginx/1.12.2
Date: Tue, 10 Dec 2019 08:05:06 GMT
Connection: keep-alive
Location: http://aa.com
Strict-Transport-Security: max-age=31536000

HTTP/1.1 500 Internal Server Error
Server: nginx/1.12.2
Date: Tue, 10 Dec 2019 08:05:06 GMT
Content-Type: application/json;charset=ISO-8859-1
Connection: keep-alive
X-Artifactory-Id: 6acaec07491b1fc7:67bdf17c:16ec78faeb7:-8000
---------------
can not find  302 for redirect then take a alphabetic and redirect to aa.com
nociSoftware Engineer
Distinguished Expert 2018

Commented:
The problem is aa.com responds with  500 error.
So the redirect (-L) / 302 response works, The followup on the next server fails.

apparantly the aa.com server has a misconfiguration reaching some internal backend for url: http://aa.com/

Can you explain what you need?

Author

Commented:
i removed aa.com and restart nginx . for http://web-server.com.
i get "
This page isn’t working web-server redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS"
i move aa to tt.com and restart nginx result is the same  "this page isn't ..."
if i just rename it like alphabetic before website-com it's open this page otherwise say "this page ..."
nociSoftware Engineer
Distinguished Expert 2018

Commented:
1) what do you want to accomplish?


if there is nothing handling stuff: redirect http://web-server.com -> http://web-server.com   will get you too many redirects.
if aa.com points to the same web server and  redirect http://web-server.com -> aa.com then the server still needs to handle aa.com
( for example keep it simple and put a file index.html in the top level directory and put a text inside like: your are in aa.com now).

Author

Commented:
what i want is redirect http://web-server.com  to https://web-server.com 
right now if http://web-server redirect to http://aa.com and if i remove aa its says: "This page isn't ..."
Software Engineer
Distinguished Expert 2018
Commented:
Ah ok:

   server {
      listen :80 ;
      listen [::]:80  ;
      server_name web-server.com ;

      return 301 https://$host$request_uri
   }

Open in new window


or
server {
  listen 80 ;
  listen [::]:80 default_server;
  server_name _;   # match ANY hostname...
  server_tokens off;

  ## Redirects all traffic to the HTTPS host
  root /nowhere; ## root doesn't have to be a valid path since we are redirecting
  rewrite ^ https://$host$request_uri? permanent;
}

Open in new window


The https in the target URL is crucial otherwise it will return to the same spot.
redirect from http://web-server.com -> http://web-server.com WILL create a loop. The target needs to be different from the source.

Author

Commented:
i don't understand i have had the same as your second suggestion (except for root/... and i have 301 for rewrite) and change to what you suggest now it's working  THANK YOU

Author

Commented:
sorry i was fast. it's not solved  yet.
now curl -I -L  http://web-server.com return :
-------------
"HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 11 Dec 2019 13:17:49 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 17 Oct 2017 13:16:37 GMT
Connection: keep-alive
ETag: "59e602b5-264"
Accept-Ranges: bytes"
------------
and from browser http://web-server.com return nginx default page . i removed default page from site-avi.. and site-enable.
where default page coming from? :-( .
nociSoftware Engineer
Distinguished Expert 2018
Commented:
Was the other (old) entry removed?  The 2nd example with _ hostname  is a catch all.
So any other port 80 entry will not conflict and other will go first.  

You may want to replace the _ with the actual sitename.

I guess the default page comes from another entry. (or the redirect was successful and is now deliverd from the https:/./  part of the site.
(gain the _ will match any sitename you have.).

Author

Commented:
solved. Thanks