M
M
max_mara2011-08-11 18:56:58
Nginx
max_mara, 2011-08-11 18:56:58

Nginx redirect from http to https?

Good afternoon
I want to use only https and I want to redirect from http to https
I do this

server {<br/>
 listen *:80;<br/>
 server_name example.com;<br/>
 rewrite ^(.*)$ https://$server_name$1 permanent;<br/>
}<br/>
<br/>
server {<br/>
 listen *:443;<br/>
 ...<br/>
}<br/>

and when going to example.com
The plain HTTP request was sent to HTTPS port
https works fine
I use enginx as a front-end, apache as a back-end.
I want all connections to nginx to be encrypted, and between nginx apache is already a regular http.
What am I doing wrong? Is there any other way to solve the problem at hand?

Answer the question

In order to leave comments, you need to log in

10 answer(s)
I
ilnarb, 2011-08-12
@max_mara

and if so?

server {
  listen *:80;
  server_name example.com;
  proxy_set_header Host example.com;
  location / {
    rewrite ^(.*)$ https://example.com$1 permanent;
  }
}
server {
  listen *:443 ssl;
  server_name example.com;
  proxy_set_header Host example.com;
  location / {
    proxy_pass http://127.0.0.1:8080;
  }
}

M
metajiji, 2014-08-26
@metajiji

According to the official documentation , it is recommended to use the following construction:

server {
        listen 80;
        server_name example.com;
        return 301 https://$server_name$request_uri;  # enforce https
#        rewrite ^(.*) https://www.example.com$uri permanent;
}

N
nikitasius, 2014-08-26
@nikitasius

another good option:

if ($ssl_protocol = "") {                                
                                rewrite ^/(.*) https://$server_name/$1 permanent;
}

I have such code for certain location made. For example, so that admin and others like them only work through https.

X
XHunter, 2011-08-11
@XHunter

Do this:
rewrite ^(.*) https://$server_name$1 permanent;

Y
Yuri, 2011-08-11
@xtreme

Damn, I started writing about a redirect in the code, and for some reason I remembered about .htaccess. It turned out ugly :)
In general, you need to look for where 443 stands next to http://

V
V2NEK, 2013-11-27
@V2NEK

Correct to
listen *:443 ssl;

M
mastini, 2011-08-11
@mastini

Try this:
rewrite ^(.*) https://$host$1 permanent;

Y
Yuri, 2011-08-11
@xtreme

Just for the sake of experiment, I did such a redirect. Moreover, with the letter-for-letter given config, with the exception of server_names. Everything works perfectly. Apparently the problem is not in this part, but further, for example, in the description of proxying to Apache.
proxy_redirect off; didn't forget?

M
Michael, 2011-08-11
@1099511627776

>400 Bad Request The plain HTTP request was sent to HTTPS port
I think it's just that the browser itself doesn't know to send an encrypted request. and sends encrypted. Maybe you should try to make a redirect through the PHP
engine itself :

header('Location: https://.........../');

D
dabster, 2016-09-20
@dabster

do not forget to add SetEnvIf X-Forwarded-Proto https HTTPS=on to Apache virtualhosts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question