每当我尝试在 NGINX 配置文件中添加重定向时,我都会收到此错误,因此我基本上避免了它,这不是真正继续的方法,所以我需要弄清楚为什么我不能添加重定向...当我需要重定向网站以仅使用 HTTPS 时,这主要是一个问题。
我当前的基础设施由 Nginx (8080) 和 Varnish(80) 组成,服务器托管多个其他网站作为虚拟主机,我的配置几乎都一样。看在上帝的份上,请有人告诉我如何追踪这个问题,我了解服务器,但这是一个真正的独角兽问题,它不断出现,我想使用 NGINX 作为我的主要网络服务器,但这确实给工作带来了麻烦。我已将我的 Wordpress WP 配置设置为包含由 varnish 发送的 proto 标头。我还在 wordpress 中将站点名称设置为 https 版本,并将数据库中的每个项目 url 更改为 https,因此理论上默认情况下所有内容都应该是 https
if (isset($_SERVER["HTTP_X_FORWARDED_PROTO"] ) && "https" == $_SERVER["HTTP_X_FORWARDED_PROTO"] ) {
$_SERVER["HTTPS"] = "on";
}
我的 HTTP 8080:
server {
listen 8080;
server_name example.com www.example.com;
index index.html index.php;
root /var/www/example.com;
port_in_redirect off;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 5;
gzip_buffers 16 8k;
gzip_http_version 1.0;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/png image/gif image/jpeg application/javascript image/svg+xml;
location = / {
alias /var/www/example.com/;
fastcgi_param HTTPS on;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass backend;
}
location ~ /\.ht {
deny all;
}
return 301 https://www.example.com$request_uri;
}
我的 HTTPS 配置:
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'kEECDH+ECDSA+AES128 kEECDH+ECDSA+AES256 kEECDH+AES128 kEECDH+AES256 kEDH+AES128 kEDH+AES256 DES-CBC3-SHA +SHA !aNULL !eNULL !LOW !kECDH !DSS !MD5 !EXP !PSK !SRP !CAMELLIA !SEED';
ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
ssl_dhparam /etc/ssl/certs/dhparam.pem;
}
答案1
您需要将 SSL 终止器配置为反向代理:
在你的 nginx ssl 配置中,添加以下内容:
location / {
proxy_pass http://VARNISH-IP-ADDR-OR-HOSTNAME;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Nginx on;
proxy_redirect off;
}
然后,在你的 varnish vcl 文件中
if ( req.http.X-Nginx != "on") {
return (synth(750, ""));
}
...
sub vcl_synth {
if (resp.status == 750) {
set resp.status = 301;
set resp.http.Location = "https://YOUR-SSL-FQDN" + req.url;
return(deliver);
}
}
最后:对于 wordpress,在 wp-config.php 中添加以下几行:
define('FORCE_SSL_ADMIN', true);
$_SERVER['HTTPS']='on';
注意:我假设您的其他 varnish 配置良好,并且您的 nginx“8080”配置适合您的 php 应用程序。