S
S
sharkynumberone2018-08-20 17:40:11
Nginx
sharkynumberone, 2018-08-20 17:40:11

How to redirect from www to non-www and from http to https in nginx proxy?

The task seems to be simple. There is Nginx reverse proxy and host config
server {
listen 80;
server_name www.demo.ru demo.ru;
return 301 https://$server_name$request_uri;
}
server {
listen 443;
server_name demo.ru www.demo.ru;
ssl_certificate /etc/nginx/certs/demo.ru.crt;
ssl_certificate_key /etc/nginx/certs/demo.ru.key;
ssl on;
ssl_session_cache builtin:1000shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://demo_web;
}
}
When trying to access the site via https://www.demo.ru , it returns 200 status, although there should be a redirect to https://demo.ru. Ultimately, I need demo.ru www.demo.ru https://www.demo.ru to have a redirect to https://demo.ru . Where is the mistake?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
H
Hikmat Abdunabiev, 2018-08-20
@Khikmat

You have a redirect to http and you go to https. Try logging in with http.

A
Andrey Shatokhin, 2018-08-20
@Sovigod

You don't have a redirect to without www.
add before location /
It is also not necessary to make a separate server for redirecting to https. Enable both ports (80/443) on one and add a similar redirect

if ($scheme = 'http') { return 301 https://demo.ru$request_uri;}

T
TyzhSysAdmin, 2018-08-20
@POS_troi

When trying to enter the site through https://www.demo.ru returns 200 status, although there should be a redirect.

There shouldn't be a redirect, you don't have a redirect set up from the www subdomain.
You need to add a separate block
server{
  server_name www.demo.ru;
  return 301 $scheme://www.demo.ru$request_uri;
}

well, or bring the existing block to the form
server {
listen 80;
server_name www.demo.ru demo.ru;
return 301 https://www.demo.ru$request_uri;
}

well, or pervert and cut www with regexps :)

Z
zevilz, 2018-08-31
@zevilz

I usually do this:

server {
  listen 80;
  server_name site.com www.site.com;
  rewrite ^(.*) https://$server_name$1 permanent;
}
server {
  listen 443 ssl;
  include ssl/site.com;
  server_name www.site.com;
  rewrite ^(.*) https://site.com$1 permanent;
}
server {
  listen 443 ssl;
  include ssl/site.com;
  server_name site.com;
  ...........
  ...........
  ...........
}

In the first section redirect http with www and http without www to the same but https.
In the second section, redirect https from www to https without www (a certificate must be connected here).
In the third section, everything else is for the domain (https without www).

I
Igor, 2015-02-10
@hurgadan

why do you have MAX(ggh.timemodified) and accordingly GROUP BY in your query? if you just need to select records by the timemodified<=1419933851 condition, grouping is not needed.
instead
of MAX(ggh.timemodified) it will be ggh.timemodified
and GROUP BY c.fullname remove

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question