gpt4 book ai didi

linux - 在 Nginx 上将 http 重写为 https

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

我尝试将 http://localhost 重写为 https://localhost

我读了一些关于此问题的答案,当我在本地服务器上尝试它时它正在工作。我用这些行来做到这一点:

server {
listen 80;

rewrite ^(.*) https://$server_name$request_uri permanent;
[...]
}

但是当我来自另一台具有如下 IP 的计算机时:xxx.xxx.xxx.xxx:8086 它会将我重定向到 https://localhost,但我不希望这样。

所以,我尝试这样:

server {
listen 80;

rewrite ^(.*) https://$server_name$request_uri permanent;
[...]
}

另一个问题:它在没有我的 :8086 的情况下将我重定向到 https://xxx.xxx.xxx.xxx !这也不是我想要的...

有人可以帮我解决吗?

非常感谢!

马克西姆。

编辑:

我的配置:

server {


listen 80;


#rewrite ^(.*) https://$host:8086$request_uri permanent;

root /var/www;
rewrite ^ https://$server_name$request_uri permanent;
index index.php index.html index.htm;

location / {
try_files $uri $uri/ /index.html;
}

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;
include fastcgi_params;
}
}

server {


listen 443 ssl;
server_name localhost;

ssl on;
ssl_certificate ./cert.crt;
ssl_certificate_key ./cert.key;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

root /var/www;
index index.php index.html index.htm;

location / {
try_files $uri $uri/ /index.html;
}

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;
include fastcgi_params;
}
}

在我的 rooter 的配置中,该服务器的端口重定向是 8086。

最佳答案

您无法将 http 重定向到同一端口上的 https。它们必须监听不同的端口,否则您将遇到重写循环。

例如:

server { 
listen 80;
server_name example.com;
root /nowhere; # this doesn't have to be a valid path since we are redirecting, you don't have to change it.
rewrite ^ https://$server_name$request_uri permanent;
}

server {
listen 443 ssl;
server_name example.com;
root /some/valid/directory;

ssl on;
}
<小时/>

编辑完整配置:

只是为了澄清您想要的:

        World
|
|
http://localhost:8086
|
|
https://localhost:8086
|
|
https://localhost:443

为此,您需要使用 proxy_pass。另外,nginx 错误代码 497 可能会有所帮助:http://nginx.org/en/docs/http/ngx_http_ssl_module.html#errors

将以下服务器指令添加到您的配置中...

server { 
listen 8086 ssl;
ssl on;
ssl_certificate ./cert.crt;
ssl_certificate_key ./cert.key;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

error_page 497 https://$host:$server_port$request_uri;
location / {
proxy_pass https://localhost:443;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Ssl on;
}
}

关于linux - 在 Nginx 上将 http 重写为 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26909481/

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