Pecha89
@Pecha89
web developer

Как настроить nginx тобы порт 80 работал и на http и на https?

Всем привет, не очень хорошо разбираюсь в настройки nginx, подскажите плиз как настроить конфиг nginx чтобы сайт работал с портом example:80 с https, тоесть https://example.ru:80/
Дело в то что когда у меня такие настройки, и прописано listen 68.183.39.71:80 ssl ;, то 80 порт работает на https, но не работает переадресация с http на https( любые ссылки с http не редиректят на https а открываются с ошибкой 400 bad rquest
server {
    server_name example.ru www.example.ru  ;
    listen 68.183.39.71:80 ssl ; <---- тут
    listen 68.183.39.71:443 ssl ;

    ssl_certificate "/var/www/httpd-cert/example.ru_2020-11-13-13-03_46.crt";
    ssl_certificate_key "/var/www/httpd-cert/example.ru_2020-11-13-13-03_46.key";

    charset utf-8;
    gzip on;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/css text/xml application/javascript text/plain application/json image/svg+xml image/x-icon;
    gzip_comp_level 1;

    set $root_path /var/www/example.ru/data/www/example.ru;
    root $root_path;
    disable_symlinks if_not_owner from=$root_path;

    location / {

        proxy_pass http://127.0.0.1:81;
        proxy_redirect http://127.0.0.1:81/ /;
        include /etc/nginx/proxy_params;
    }


     location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpeg|avi|zip|gz|bz2|rar|swf|ico|7z|doc|docx|map|ogg|otf|pdf|tff|tif|txt|wav|webp|woff|woff2|xls|xlsx|xml)$ {
        try_files $uri $uri/ @fallback;
    }

    location @fallback {
        proxy_pass http://127.0.0.1:81;
        proxy_redirect http://127.0.0.1:81/ /;
        include /etc/nginx/proxy_params;
    }

    include "/etc/nginx/fastpanel2-sites/example.ru/example.ru.includes";
    include /etc/nginx/fastpanel2-includes/*.conf;
    error_log /var/www/example.ru/data/logs/example.ru-frontend.error.log;
    access_log /var/www/example.ru/data/logs/example.ru-frontend.access.log;
}


server {
    server_name example.ru www.example.ru  ;
    listen 68.183.39.71:80;
    return 301 https://$host$request_uri;

    error_log /var/www/example.ru/data/logs/example.ru-frontend.error.log;
    access_log /var/www/example.ru/data/logs/example.ru-frontend.access.log;
}
  • Вопрос задан
  • 286 просмотров
Решения вопроса 1
saboteur_kiev
@saboteur_kiev
software engineer
Нельзя так делать.
Для http и https нужно разные порты настроить, и делать редирект с 80 порта на https 443

server {
    listen 80 default_server;

    server_name _;

    return 301 https://$host$request_uri;
}
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 2
Pecha89
@Pecha89 Автор вопроса
web developer
в итоге так настроил
server {
    server_name example.ru www.example.ru  ;
    listen 68.183.39.71:443 ssl;

    ssl_certificate "/var/www/httpd-cert/example.ru_2020-11-13-13-03_46.crt";
    ssl_certificate_key "/var/www/httpd-cert/example.ru_2020-11-13-13-03_46.key";

    charset utf-8;
    gzip on;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/css text/xml application/javascript text/plain application/json image/svg+xml image/x-icon;
    gzip_comp_level 1;

    set $root_path /var/www/example.ru/data/www/example.ru;
    root $root_path;
    disable_symlinks if_not_owner from=$root_path;

    location / {

        proxy_pass http://127.0.0.1:81;
        proxy_redirect http://127.0.0.1:81/ /;
        include /etc/nginx/proxy_params;
    }


     location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpeg|avi|zip|gz|bz2|rar|swf|ico|7z|doc|docx|map|ogg|otf|pdf|tff|tif|txt|wav|webp|woff|woff2|xls|xlsx|xml)$ {
        try_files $uri $uri/ @fallback;
    }

    location @fallback {
        proxy_pass http://127.0.0.1:81;
        proxy_redirect http://127.0.0.1:81/ /;
        include /etc/nginx/proxy_params;
    }

    include "/etc/nginx/fastpanel2-sites/example.ru/example.ru.includes";
    include /etc/nginx/fastpanel2-includes/*.conf;
    error_log /var/www/example.ru/data/logs/example.ru-frontend.error.log;
    access_log /var/www/example.ru/data/logs/example.ru-frontend.access.log;
}

server {
    server_name example.ru www.example.ru  ;
    listen 68.183.39.71:80 ssl;
    return 301 https://$host$request_uri;

    ssl_certificate "/var/www/httpd-cert/example.ru_2020-11-13-13-03_46.crt";
    ssl_certificate_key "/var/www/httpd-cert/example.ru_2020-11-13-13-03_46.key";
}

server {
    server_name example.ru www.example.ru ;
    listen 68.183.39.71:80;
    return 301 https://$host$request_uri;

    error_log /var/www/example.ru/data/logs/example.ru-frontend.error.log;
    access_log /var/www/example.ru/data/logs/example.ru-frontend.access.log;
}
Ответ написан
Комментировать
вы RFC ваще читать умеете ?
вы новый интернет зачем придумываете?
редирект на https, все !
Ответ написан
Ваш ответ на вопрос

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

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