【问题标题】:redirect http to https with nginx: gives to http 'This site can’t be reached'使用 nginx 将 http 重定向到 https:给 http '无法访问此站点'
【发布时间】:2021-07-13 02:08:17
【问题描述】:

我有一个使用 gunicorn 和 nginx 运行的烧瓶应用程序。 https正常工作。

我真的很想找到一种将 http 重定向到 https 的方法。我在互联网上尝试了多种解决方案,但似乎都不适用于我的情况。

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

附加信息:

  • nginxflask app with gunicorn 在两个不同的容器上运行,我使用 docker-compose 构建。
  • https 按预期工作,但 http 显示“无法访问此站点”

知道我的配置文件有什么问题吗?任何见解都可能会有所帮助。谢谢。

编辑: 还分享 docker-compose 以防万一那里出现问题:

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

【问题讨论】:

    标签: nginx flask docker-compose gunicorn


    【解决方案1】:

    我的 http 配置如下,效果很好。

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

    你还应该删除你的第三个服务器块。

    【讨论】:

    • 感谢您的回答。它对我不起作用。它也打破了https。
    • 很遗憾听到这个消息。你做过配置检查吗?应该是 sudo nginx -t 取决于您的平台
    • 我在公开端口 80 的位置共享了我的 docker-compose 文件。当我还公开端口 80 时,它可以工作。但是我得到 www.example.com// (最后有两个斜杠,我不知道它来自哪里)
    【解决方案2】:

    这是因为在你的 docker-compose 文件中你只设置了 nginx 监听端口 443,这样来自端口 80 的任何请求都无法访问你的应用程序。

    您的 docker-compose 应该如下所示:

    nginx: 重启:总是 构建:./nginx 端口: - “80:80” - “443:443” 取决于: - 网站容器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-29
      • 2018-01-05
      • 1970-01-01
      • 2018-07-11
      • 2021-05-11
      • 2014-10-11
      • 2018-08-18
      • 2017-02-25
      相关资源
      最近更新 更多