【发布时间】:2018-10-25 11:00:37
【问题描述】:
我正在使用 varnish 缓存服务器和 nginx。我试图从http重定向到https。我在 varnish 服务器中编写了将 http 重定向到 https 的配置。
default.vcl
sub vcl_recv {
if (client.ip != "127.0.0.1" && req.http.host ~ "groundforce.cloud") {
set req.http.x-redir = "https://groundforce.cloud" + req.url;
return(synth(850, ""));
}
}
sub vcl_synth {
if (resp.status == 850) {
set resp.http.Location = req.http.x-redir;
set resp.status = 302;
return (deliver);
}
}
我的nginx配置文件:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name my_server;
port_in_redirect off;
ssl on;
ssl_certificate /etc/ssl/my_server.crt;
ssl_certificate_key /etc/ssl/my_server.key;
ssl_session_cache shared:SSL:20m;
ssl_session_timeout 60m;
add_header Strict-Transport-Security "max-age=31536000";
add_header X-Content-Type-Options nosniff;
location / {
proxy_pass http://127.0.0.1:80;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header HTTPS "on";
}
}
server {
listen 8080;
listen [::]:8080;
server_name my_server;
root /var/www/html;
index index.php;
port_in_redirect off;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS on;
fastcgi_pass unix:/var/run/php7.0-fpm.sock;
}
}
注意:我删除了上面的 default.vcl 代码,然后 http 和 https 都可以正常工作。我关注了以下article。
【问题讨论】: