我有一台在 DigitalOcean 上运行的服务器,并且有一个我购买的域名。 我的服务器上运行着 nginx,我试图将我的 http 请求重定向到 https,但我尝试过的任何方法都不起作用。 我知道它应该非常简单,我已经查看了数十个示例,但由于某种原因我的仍然无法正常工作。
现在这就是我所拥有的
server {
listen 80;
server_name www.example.com example.com;
listen [::]:80;
return 301 https://$host$request_uri;
}
server {
# SSL configuration
#
listen 443 ssl;
# listen [::]:443 ssl;
root /var/www/example.com;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
server_name example.com www.example.com;
ssl_certificate_key /etc/nginx/keys/example.key;
ssl_certificate /etc/nginx/keys/example.com.chained.crt;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
}
这有什么问题或者我应该改变什么吗? 我的网站运行良好,并且浏览器中显示锁定图标,表明如果我输入 https://example.com 或 https://www.example.com
,该网站是安全的但是如果我只使用 example.com http://example.com 或 www.example.com 它说找不到该页面。
示例脚本 nginx :
server
{
listen 443 default ssl;
listen [::]:443 ssl;
root /var/www/html/api_mobile/public;
include snippets/ssl-params.conf;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.html index.php index.htm index.nginx-debian.html;
server_name sitename.com;
charset utf-8;
location / {
if ($scheme ="http") {
# redirect all non api traffic to https block
return 301 https://$server_name$request_uri;
}
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
ssl_certificate /etc/letsencrypt/live/sites/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/site/privkey.pem; # managed by Certbot
}
关于计划条件:
if ($scheme ="http")
{
# redirect all non api traffic to https block
return 301 https://$server_name$request_uri;
}
它在一个端口 443 https 上重定向的核心强制 http 到 https