配置 HTTPS 重定向失败



我在digitalocean上托管了一个Django应用程序。我按照本教程完成其SSL认证。按照该教程,我不知道在哪里添加这行代码:

return 301 https://$server_name$request_uri;

我尝试将其添加到/etc/nginx/sites-enabled/leptitox_pro

server {
listen 80;
server_name 68.183.203.33 yahkut.com www.yahkut.com;
return 301 https://$server_name$request_uri;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/leptitoxadmin/pyapps/Leptitox;
}
location /media/ {
root /home/leptitoxadmin/pyapps/Leptitox;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}

当它不起作用时,我将其添加到/etc/nginx/sites-available/leptitox_pro

server {
listen 80;
server_name 68.183.203.33 yahkut.com www.yahkut.com;
return 301 https://$server_name$request_uri;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/leptitoxadmin/pyapps/Leptitox;
}
location /media/ {
root /home/leptitoxadmin/pyapps/Leptitox;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}

它在那里也不起作用,所以我在/etc/nginx/nginx.conf的服务器代码块下面添加了:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
server {                                           # new
listen 80;                                       # new
server_name yahkut.com;                          # new
return 301 https://$server_name$request_uri;     # new
}
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
}

然后我重新启动了ngnix并运行了nginx -t并得到了一条成功消息,当我运行该网站时,我得到了该网站的404 not found或不安全版本。 请帮我解决这个问题。谢谢

您必须将运行端口 80 的服务器块和运行端口 443 (SSL( 的服务器块分开。就像这样:

server {
listen 80;
server_name 68.183.203.33 yahkut.com www.yahkut.com;
return 301 https://$server_name$request_uri;
# Stop here, it's will be redirect to HTTPS. There's no left to execute
}
server {
listen 443 ssl;
server_name yahkut.com www.yahkut.com;
ssl_certificate /path/to/certificate/your_domain_chain.crt;
ssl_certificate_key /path/to/your_private.key;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/leptitoxadmin/pyapps/Leptitox;
}
location /media/ {
root /home/leptitoxadmin/pyapps/Leptitox;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}

添加这些服务器块。

这是将 http 重定向到 https

server {
listen 80;
server_name example.com; 
location / {
return 301 https://$host$request_uri;
}
}

您的主块与 SSL

server {
listen 443 ssl ;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; 
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;  
index index.html index.htm index.nginx-debian.html;
server_name example.com; 
location / {
proxy_pass http://localhost:5003; // Your port goes here
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;
}
}

这位于/nginx/sites-enabled/default 下,或者您可以在此文件夹中为其创建不同的文件

相关内容

  • 没有找到相关文章

最新更新