I
I
IliaMal2020-03-29 07:48:09
linux
IliaMal, 2020-03-29 07:48:09

Why doesn't redirect work in nginx configs?

Hello.

It is necessary to organize a redirect to https for several domains.
On the first site1.ru domain (the first was configured and the first in the /etc/nginx/sites-available/ directory) after making the following config settings:

server {
root /var/www/site1.ru/public_html;
index index.php index.html index.htm;
server_name site1.ru www.site1.ru;   

    listen site1.ru:443 ssl; # default_server;

    ssl_certificate /etc/letsencrypt/live/site1.ru/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/site1.ru/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/site1.ru/chain.pem;

    ssl_stapling on;
    ssl_stapling_verify on;
    resolver site1.ru 8.8.8.8;

    # exclude returning to the http-version of the site
   add_header Strict-Transport-Security "max-age=31536000";

    # Break all images in the  http://
    add_header Content-Security-Policy "img-src https: data:; upgrade-insecure-requests";

#	return 301 http://$host$request_uri;

location / {
try_files $uri /index.php$is_args$args;
}
location ~ \.php$ {
  try_files $uri =404;
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  fastcgi_pass unix:/var/run/php7.2-fpm-site1.sock;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
  fastcgi_send_timeout 333;
  fastcgi_read_timeout 333;
}

# Add key's SSL
include acme;

location ^~ /.well-known/acme-challenge/ {
  allow all;
  default_type "text/plain";
  }
}

the redirect works correctly, although server{} is not registered for port 80, site2.ru and site3.ru do not redirect.
If added to configs:
server {
  server_name site2.ru www.site2.ru;
  listen site2.ru:80;
  return 301 https://$host$request_uri;
}

Then, when you try to restart nginx, it gives the following error:
Mar 29 05:07:41 277342-cl20517.tmweb.ru nginx[25570]: nginx: [emerg] still could not bind()
Mar 29 05:07:41 277342-cl20517.tmweb.ru systemd[1]: nginx.service: Control process exited, code=exited status=1
Mar 29 05:07:41 277342-cl20517.tmweb.ru systemd[1]: nginx.service: Failed with result 'exit-code'.
Mar 29 05:07:41 277342-cl20517.tmweb.ru systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit nginx.service has failed.
--
-- The result is RESULT.

There is nothing specific in the error, I do not understand what he wants.

I removed the symlink for default in /etc/nginx/sites-enabled/, didn't help.
What could be the problem?

I will immediately attach /etc/nginx/nginx.conf:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
  worker_connections 768;
  # multi_accept on;
}

http {

  ##
  # Basic Settings
  ##

  fastcgi_cache_path /var/cache/ levels=1:2 keys_zone=MYCACHE:10m inactive=4h;
 	fastcgi_cache_key "$scheme$request_method$host$request_uri";

  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;
  # server_tokens off;

  # server_names_hash_bucket_size 64;
  # server_name_in_redirect off;

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  ##
  # SSL Settings
  ##

  ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
  ssl_prefer_server_ciphers on;

  ##
  # Logging Settings
  ##

  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  ##
  # Gzip Settings
  ##

  gzip on;

  # gzip_vary on;
  # gzip_proxied any;
  # gzip_comp_level 6;
  # gzip_buffers 16 8k;
  # gzip_http_version 1.1;
  # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

  ##
  # Virtual Host Configs
  ##

  include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-03-29
@IliaMal

what exactly nginx does not like is very easy to find out.
changed configs, do
nginx -t
if everything is fine, do
nginx -s reload
configs will be re-read, changes will be accepted.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question