@wargych

Как настроить редирект http на https в nginx-конфигурации с несколькими доменами?

Добрый день!
Есть конфиг ниже в спойлере (в первом блоке server убрал содержимое из location, чтобы меньше места занимало).
Все location работают нормально, по домену bot-test.example.ru сервис тоже нормально открывается через https (настройки во втором блоке server).
Пытаюсь сделать для всех запросов, либо для отдельных доменов редирект с http на https, но пока он не работает. (третий блок server). Хотя вроде бы, все по официальной документации.
В чем здесь ошибка и как сделать правильно?
По этой ссылке вроде все попробовал уже
конфиг

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    map $http_upgrade $connection_upgrade {
 	 default upgrade;
 	 '' close;
	}
	
    upstream botpress_server {
  server botpress-server:3000;
}

# Disable sending the server identification
server_tokens off;

# Prevent displaying Botpress in an iframe (clickjacking protection)
add_header X-Frame-Options SAMEORIGIN;

# Prevent browsers from detecting the mimetype if not sent by the server.
add_header X-Content-Type-Options nosniff;

# Force enable the XSS filter for the website, in case it was disabled manually
add_header X-XSS-Protection "1; mode=block";

# Configure the cache for static assets
proxy_cache_path /srv/nginx_cache levels=1:2 keys_zone=my_cache:10m max_size=10g
              inactive=60m use_temp_path=off;

# Set the max file size for uploads (make sure it is larger than the configured media size in botpress.config.json)
client_max_body_size 15M;
  
    server {
        listen 443 ssl;
        server_name api-test.example.ru www.api-test.example.ru;
        ssl_certificate cert.crt;
        ssl_certificate_key key.crt;

        root /var/www;
        index index.html;

        location / {
            try_files $uri $uri/ =404;
        }
        
       location /pgadmin/ {
       ...
  }
       
       
        
  location /consul/ {
	...
  }
 
       
  location /bus/ {
     ...
  }
  
  location /auth/ {
     ...
  }
  
  location /grafana/ {
   ...
  }

  # Proxy Grafana Live WebSocket connections.
  location /grafana/api/live {
 ...
  }
  
  location /integration/mapping {
      ...
  }
       
  location /token {
      ...
  }
  
         
 }
 
  server {
        listen 443 ssl;
        server_name bot-test.example.ru www.bot-test.example.ru;
	ssl_certificate cert.crt;
        ssl_certificate_key key.crt;    
        root /var/www;
        index index.html;
 # Enable caching of assets by NGINX to reduce load on the server
  location ~ .*/assets/.* {
    proxy_cache my_cache;
    proxy_ignore_headers Cache-Control;
    proxy_hide_header Cache-Control;
    proxy_hide_header Pragma;
    proxy_pass http://botpress-server:3000;
    proxy_cache_valid any 30m;
    proxy_set_header Cache-Control max-age=30;
    add_header Cache-Control max-age=30;
  }

  # We need to add specific headers so the websockets can be set up through the reverse proxy
  location /socket.io/ {
    proxy_pass http://botpress-server:3000/socket.io/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
  }

  # All other requests should be directed to the server
  location / {
    proxy_pass http://botpress-server:3000;
  }
}
server {
server_name  bot-test.example.ru www.bot-test.example.ru;
return 301 https://$server_name$request_uri;
}
}



UPD: все оказалось проще, порт 80 был закрыт вне nginx.
Конфигурация с предложенным решением от Drno работает
  • Вопрос задан
  • 128 просмотров
Решения вопроса 1
@Drno
Для всех в самый верх
#Redirect All HTTP
server {
listen 80;
server_name _;
return 301 https://$host$request_uri;
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы