S
S
serega 20702020-12-09 17:33:15
Nginx
serega 2070, 2020-12-09 17:33:15

How to configure nginx to work on port 80 on both http and https?

Hello everyone, I’m not very good at nginx settings, please tell me how to set up the nginx config so that the site works with the example: 80 port with https, i.e. https://example.ru:80/
The fact is that when I have such settings, and written listen 68.183.39.71:80 ssl ; , then port 80 works on https, but redirection from http to https does not work (any links from http do not redirect to https but open with a 400 bad rquest error

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;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Saboteur, 2020-12-09
@pecha89

You can't do that.
For http and https, you need to configure different ports, and redirect from port 80 to https 443

server {
    listen 80 default_server;

    server_name _;

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

S
serega 2070, 2020-12-09
@Pecha89

finally set it up like this

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;
}

V
Viktor Taran, 2020-12-09
@shambler81

you finally know how to read RFC?
why are you inventing the new internet?
redirect to https, that's it!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question