如何将 apache 上的 http://example.com 网站重定向到 nginx 上的 https://www.example.com 网站。
谢谢
首先,您必须为 nginx 网站的 www.example.com 传输添加 A 记录(在 DNS 配置中)。但这对于客户和 SEO 整体来说并不好。第二个A记录必须是apache网站上的example.com。
添加 DNS 记录之后或之前,您必须检查 example.com 的 Apache 配置 - apache 网站必须仅适用于 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 apimobile.imeconteknindo.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