【问题标题】:Django+uwsgi+nginx+Lets encrypt can't access httpsDjango+uwsgi+nginx+Lets encrypt 无法访问https
【发布时间】:2018-08-08 16:27:27
【问题描述】:

[已解决]

它是由 /etc/nginx/sites-enabled/default 引起的

默认文件已经定义了绑定流量,所以当我删除它时,它工作正常。


我正在使用 Django/uwsgi/nginx。

为了访问 ssl,安装了 Lets encrypt。

下面的源码是nginx和uwsgi的配置文件。

[project_rest.conf]

upstream django {t
    server 127.0.0.1:8001;
}

server {
    listen      8000;
    server_name .mysitedomain.com;
    charset     utf-8;

    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /home/app/project_rest/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/app/project_rest/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
       uwsgi_pass  django;
        include     /home/app/project_rest/uwsgi_params; # the uwsgi_params file you installed
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mysitedomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mysitedomain.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

}

(我创建了 project_rest.conf 并链接到 /etc/nginx/sites-enabled/)

[/etc/nginx/sites-available/default]

server {
        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name mysitedomain.com www.mysitedomain.com;

        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #   include snippets/fastcgi-php.conf;
        #
        #   # With php7.0-cgi alone:
        #   fastcgi_pass 127.0.0.1:9000;
        #   # With php7.0-fpm:
        #   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #   deny all;
        #}

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mysitedomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mysitedomain.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

}

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


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


    listen 80 default_server;
    listen [::]:80 default_server;

    server_name mysitedomain.com www.mysitedomain.com;
    return 404; # managed by Certbot
}

[uwsgi.ini]

[uwsgi]
# the base directory (full path)
chdir=/home/app/project_rest
# Django's wsgi file
module=project_rest.wsgi:application
master=true
# maximum number of worker processes
processes=10
# the socket (use the full path to be safe
socket=127.0.0.1:8001
chmod-socket=664
chown-socket=app:app
pidfile=/tmp/project_rest.pid
# clear environment on exit
vacuum=true
max-requests=5000
daemonize=project_rest.uwsgi.log

(我使用了 vitualenv) 输入“uwsgi --ini uwsgi.ini”后,我可以访问 mysitedomain.com:8000 到我的 django 站点。 但是我无法访问https://mysitedomain.com:8000,而可以访问https://mysitedomain.com 我想访问https://mysitedomain.com:8000,如何实现? 谢谢。

【问题讨论】:

    标签: django ssl nginx


    【解决方案1】:
    server {
        listen          80;
        server_name     example.com;
        rewrite ^/(.*)  https://example.com/$1 permanent;
    }
    
    server {
        listen          443 ssl;
        server_name     example.com;
        access_log      /var/log/nginx/example.com_access.log combined;
        error_log       /var/log/nginx/example.com_error.log error;
    
        ssl_certificate /etc/letsencrypt/live/mysitedomain.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/mysitedomain.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 /static/ {
            alias /webapps/example/static/;
        }
    
        location /media/ {
            alias /webapps/example/media/;
        }
    
        location / {
            proxy_pass         http://localhost:8000/;
            proxy_redirect     off;
    
            proxy_set_header   Host              $http_host;
            proxy_set_header   X-Real-IP         $remote_addr;
            proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
        }
    
    }
    

    请使用您的域更改值并报告反馈

    【讨论】:

    • 你的意思是删除project_rest.conf的所有代码并替换成你的代码?
    • 更改代码后,我重新启动了 nginx。没关系。但是还是无法访问 https://mydomain.com:8000
    • 你改过server_name并重写了吗? proxy_pass 也应该保留在本地主机上。
    • 是的。我更改了 server_name 并重写为我的真实域并将 proxy_pass 保留为 localhost:8000。
    【解决方案2】:

    [已解决]

    它是由 /etc/nginx/sites-enabled/default 引起的

    默认文件已经定义了绑定流量,所以当我删除它时,

    效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-17
      • 2019-03-03
      • 2015-11-08
      • 2017-05-16
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 2014-02-01
      相关资源
      最近更新 更多