V
V
vekozlov2020-08-11 03:49:52
Nginx
vekozlov, 2020-08-11 03:49:52

Redirect with nginx + upstream. How?

Comrades, tell me how to configure nginx.

Task:
1. make a redirect from www to without-www
I found a piece of code (it works on another server), but it doesn't work in production. Where to poke him?

server {
server_name www.DOMAINDOMAIN.ru;
return 301 https://DOMAINDOMAIN.ru$request_uri ;
}

2. Perform a 301 redirect from https://questplanet.ru//// to https://questplanet.ru
That is, make a redirect with any number of slashes (multiple slashes).

3. It is also worth correcting the moment with a slash; all internal pages should have it, but the main one should not have it.

Example:
https://DOMAINDOMAIN.ru
https://www.DOMAINDOMAIN.ru/vip/

upstream app_server {
  server unix:/home/deployer/projects/DOMAINDOMAIN/shared/unicorn/DOMAINDOMAIN.sock  fail_timeout=0;
}

server {
  root /home/deployer/projects/DOMAINDOMAIN/current/public;
  #server_name 92.53.120.72;

  server_name DOMAINDOMAIN.ru; # removed ww version
  keepalive_timeout 5;
  client_max_body_size 50m;

  index index.htm index.html;

  location / {
    try_files $uri/index.html $uri.html $uri @app;
  }

  location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
    try_files $uri @app;
    expires 1y;
    log_not_found off;
  }

  location @app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://app_server;
  }

  location ~* .(woff2|woff|otf|css|js)$ {
    expires 30d;
  }

  error_page 500 502 503 504 /500.html;

  location = /500.html {
    root /home/deployer/projects/DOMAINDOMAIN/current/public;
  }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/DOMAINDOMAIN.ru/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/DOMAINDOMAIN.ru/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}


server {
  if ($host = www.DOMAINDOMAIN.ru) {
    return 301 https://$host$request_uri;
  } # managed by Certbot

  if ($host = DOMAINDOMAIN.ru) {
    return 301 https://$host$request_uri;
  } # managed by Certbot

  if ($request_uri ~ "^\/(.*)[^\/]$") {
    return 301 $1/;
  }

  listen 80 default deferred;
  server_name DOMAINDOMAIN.ru;# www.DOMAINDOMAIN.ru;
  return 404; # managed by Certbot
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Koryukov, 2020-08-11
@MadridianFox

Your main server section listens on 443, and you inserted the new redirect rules into the server that listens on 80 and is designed for http -> https
redirects. even if it works, it will cause an infinite redirect.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question