gpt4 book ai didi

linux - nginx force SSL no-www 重定向问题

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:56 25 4
gpt4 key购买 nike

过去几天我一直在努力让它正常工作,但我做不到。

(stackoverflow 不允许我发布两个以上的实际链接——忽略反斜杠)

通常的结果是 example\.netwww.example\.net 将重定向到 http\s://example.net,而 http\s://www.example\.net 不会重定向到 http\s://example.net,另一种常见情况是来自浏览器的消息声称页面未正确重定向。

我想做的是绝对强制所有请求转到 http\s://example.net$request_uri。

            example\.net => http\s://example\.net
www.example\.net => http\s://example\.net
https://www.example\.net => http\s://example\.net

nginx 站点配置:

upstream backend {
server unix:/var/run/php5-fpm.sock;
}

server {
listen 80;
server_name www.example\.net example.net;
return 301 http\s://example\.net$request_uri;
}

server {
listen 443 ssl default_server;

server_name example\.net;

include snippets/snakeoil.conf;

root /var/www/html/example;

charset utf-8;
index index.php;

access_log /var/log/example-access.log;
error_log /var/log/example-error.log;

location / {
try_files $uri $uri/ @extensionless-php;
index index.php;
}

location ~ .php {
try_files $uri =404;
fastcgi_pass backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}

location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}

谁能告诉我我做错了什么?

最佳答案

创建 2 个 block 来处理到非 www HTTPS 的重定向。最后一个 block 是您放置常用网站配置/规则的地方。

server {
listen 80;
server_name www.example.net
example.net;
return 301 https://example.net$request_uri;
}

server {
listen 443 ssl;
server_name www.example.net;
ssl_certificate path/to/cert;
ssl_certificate_key path/to/key;
return 301 https://example.net$request_uri;
}
server {
listen 443 ssl;
server_name example.net;
ssl_certificate path/to/cert;
ssl_certificate_key path/to/key;

# the rest goes here...
}

关于linux - nginx force SSL no-www 重定向问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32940048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com