Nginx
February 21, 2023

Настройка редиректа с www и http на https. Настройка логгирования

В этой статье продолжим настраивать Nginx.
Для настройки редиректа с http на https воспользуемся нижележащим фрагментом кода.

server {
    listen 80 default_server;
    listen [::]:80 default_server;   # IPv6 param
    server_name cameda.ru;
    index index.html;
    root /var/www/html/cameda.ru;

    location /.well-known {
       root /opt/www/acme;
    }

    location / {
      return 301 https://$host$request_uri;
    }
}

Для настройки редиректа с www.cameda.ru на cameda.ru добавим следующий код:

server {
    listen 80;
    server_name www.example.com;
    rewrite ^ https://example.com$request_uri permanent;
}

server {
    listen 443 ssl http2;
    server_name www.example.com;
    rewrite ^ https://example.com$request_uri permanent;
}

Также отключим все лишние логи, кроме самых критичных.

error_log /dev/null crit;

Обновим конфигурацию Nginx для настройки редиректов и отключения ненужных логов:

########################### Redirect www ###################################
server {
    listen 80;
    server_name www.cameda.ru;
    rewrite ^ https://cameda.ru$request_uri permanent;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.cameda.ru;
## SSL parameters
  ssl_certificate /etc/letsencrypt/live/cameda.ru/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/cameda.ru/privkey.pem;
  ssl_session_timeout 1d;
  ssl_session_cache shared:MozSSL:10m;  # about 40000 sessions
  ssl_session_tickets off;
  ssl_dhparam /etc/ssl/certs/dhparam.pem;
  ssl_protocols TLSv1.2 TLSv1.3;
  ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
  ssl_prefer_server_ciphers on;
  add_header Strict-Transport-Security "max-age=63072000" always;
  ssl_stapling on;
  ssl_stapling_verify on;
  resolver 127.0.0.1;
## Rewrite
  rewrite ^ https://cameda.ru$request_uri permanent;
}

############################### HTTP #######################################
server {
    listen 80 default_server;
    listen [::]:80 default_server;   # IPv6 param
    server_name cameda.ru;
    index index.php index.html;
    root /var/www/html/cameda.ru;

    location /.well-known {
       root /var/www/html/acme;
    }

    location / {
      return 301 https://$host$request_uri;
    }
}

############################### HTTPS ######################################
server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name cameda.ru;
  root /var/www/html/cameda.ru;
  access_log off;
  error_log /dev/null crit;

########################### SSL parameters #################################
  ssl_certificate /etc/letsencrypt/live/cameda.ru/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/cameda.ru/privkey.pem;
  ssl_session_timeout 1d;
  ssl_session_cache shared:MozSSL:10m;  # about 40000 sessions
  ssl_session_tickets off;
  ssl_dhparam /etc/ssl/certs/dhparam.pem;
  ssl_protocols TLSv1.2 TLSv1.3;
  ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
  ssl_prefer_server_ciphers on;
  add_header Strict-Transport-Security "max-age=63072000" always;
  ssl_stapling on;
  ssl_stapling_verify on;
  resolver 127.0.0.1;
############################################################################  
  
  location / {
    index index.php index.html;
  }
  
######################## PHP section unix socket ###########################
  location ~ \.php$ {
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
  }
  
  ######## PHP section tcp socket #################
#  location ~ \.php$ {
#    fastcgi_pass 127.0.0.1:8080;
#    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#    include fastcgi_params;
#    include snippets/fastcgi-php.conf;
#  }

############################## Let's Encrypt ###############################
# В папке /var/www/html/acme Let's Encrypt будет создавать свои файлы
  location /.well-known {
    root /var/www/html/acme;
  }
  
  location = /.well-known/acme-challenge/ {
    return 404;
  }  
############################################################################
  location ~ /\.ht {
    deny all;
  }
}
nginx -t
systemctl restart nginx

Предыдущие статьи цикла:

Установка LEMP: https://teletype.in/@cameda3/X-3jBbJDluw
Настройка SSL: https://teletype.in/@cameda3/xVsPrvNt_3U