Question

http redirect to https with proxy pass to 5000

Posted January 18, 2021 87 views
NginxUbuntuSecurityUbuntu 20.04

Hello,
I am trying to redirect my http traffic to https and also proxy_pass to port 5000… i cannot get it to work? what am i doing wrong…?

server {
        listen 80;
        listen [::]:80;

        root /var/www/this/html;
        index index.html index.htm index.nginx-debian.html;

        server_name example.com www.example.com;


    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

location / {
        proxy_pass http://localhost:5000; 
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
edited by MattIPv4

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi @andrewlintonfife,

Everything looks perfectly fine from my point of view. Anyway, here is how I usually do such configs. Please give it a try and let me know how it goes :

#This server block will redirect http:// to https://www.mysite.com
server {
    listen 80;
    listen [::]:80;
    server_name mysite.com www.mysite.com;
    return 301 https://www.mysite.com$request_uri;
}

#This server block will redirect https://mysite.com to https://www.mysite.com (expecting that you have a certificate for mysite.com as well as www.mysite.com
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    include snippets/ssl-www.mysite.com.conf;
    include snippets/ssl-params.conf;
    server_name mysite.com;
    return 301 https://www.$server_name$request_uri;
}

#This is the server block actually delivering content to the visitor
server {
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
    include snippets/ssl-www.mysite.com.conf;
    include snippets/ssl-params.conf;

    server_name www.mysite.com;

    client_max_body_size 100M;

    location ~ ^/\.well-known {
        root /var/www/ghost;
        allow all;
    }

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_buffering off;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Referer "";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
    }
}

Remember to change mysite.com with your actual domain! Once you do make the changes, please don’t forget to actually restart Nginx.

Looking forward to your reply

Regards,
KFSys

Submit an Answer