skip to Main Content

I have a flask app running with gunicorn and nginx. https works normally.

I really strangle to find a way to redirect http to https though. I have tried multiple solutions on internet but non seems to work on my case.

project.conf

server {

    listen 443 default_server;
    server_name example.com www.example.com;
    ssl on;
    ssl_certificate certs/fullchain.pem;
    ssl_certificate_key certs/privkey.pem;
    location / {
        proxy_pass http://websitecontainer:8000;
        # Do not change this
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /static {
        rewrite ^/static(.*) /$1 break;
        root /static;

    }
}

server {
 listen 80;
 server_name example.com;
 rewrite ^(.*) https://example.com/$1 permanent;
}

server {
    server_name www.example.com;
    rewrite ^(.*) https://example.com/$1 permanent;
}

nginx.conf

user  nginx;

worker_processes  1;

error_log  /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;


events {
    # Define the maximum number of simultaneous connections that can be opened by a worker process
    worker_connections  1024;
}
    
http {
    # Include the file defining the list of file types that are supported by NGINX
    include       /etc/nginx/mime.types;

    # Define the default file type that is returned to the user
    default_type  text/html;

    # Define the format of log messages.
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    # Define the location of the log of access attempts to NGINX
    access_log  /var/log/nginx/access.log  main;

    # Define the parameters to optimize the delivery of static content
    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay    on;

    # Define the timeout value for keep-alive connections with the client
    keepalive_timeout  65;

    # Define the usage of the gzip compression algorithm to reduce the amount of data to transmit
    #gzip  on;

    # Include additional parameters for virtual host(s)/server(s)
    include /etc/nginx/conf.d/*.conf;
}

Additional info:

  • nginx and flask app with gunicorn run on two different containers and I use docker-compose to build.
  • https works as expected but http gives ‘This site can’t be reached’

Any idea whats wrong with my config file? Any insight could be helpfull. Thanks.

Edit:
Sharing also the docker-compose just in case something is wrong there:

version: '2'

services:
  websitecontainer:
    build: ./webapp
    container_name: websitecontainer
    restart: always
    command: >
      gunicorn -b 0.0.0.0:8000
              --timeout 120
              --access-logfile gunicorn-access.log
              --error-logfile gunicorn-error.log
              --reload
              "app:create_app()"
    environment:
      PYTHONUNBUFFERED: 'true'
    ports:
      - '8000:8000'

  nginx:
    restart: always
    build: ./nginx
    ports:
      - "443:443"
    depends_on:
      - websitecontainer

2

Answers


  1. My configuration for http is as follows and works like a charm.

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

    Also you should delete your third server block.

    Login or Signup to reply.
  2. This happens because in your docker-compose file you only set nginx to listen to port 443, this way no request coming from port 80 won’t be able to access your application.

    Your docker-compose should look like this:

    nginx:
    restart: always
    build: ./nginx
    ports:
    – "80:80"
    – "443:443"
    depends_on:
    – websitecontainer

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search