我正在使用 nginx 和 Apache 2:
这是我在 nginx 中的配置:
server {
listen 80;
server_name hostexemple.com;
root /home/user/web/www/public;
# Redirect requests directly to Apache
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_pass http://127.0.0.1:8080;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/hostexemple.come/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/hostexemple.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
Apache 2 中的配置如下:
<VirtualHost *:8080>
ServerAdmin [email protected]
ServerName hostexemple.com
DocumentRoot /home/user/web/www/public
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/user/web/www/public/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog /home/user/logs/error-prod.log
LogLevel warn
CustomLog /home/user/logs/access-prod.log combined
</VirtualHost>
它可以工作,但我想做的是重定向来自http://hostexemple.com:8080到https://hostexemple.com以防止任何 SEO 问题。
配置将 http 重定向到 https,但没有http://hostexemple.com:8080当我尝试重定向它时,它会导致重定向循环。
我该如何修复它?
答案1
好的,找到了。我不知道这是否是正确的方法,但我分享:
在 htaccess 中我必须:
RewriteCond %{SERVER_PORT} ^8080$
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://hostexemple.com%{REQUEST_URI} [L,R=301]
为了获得更多反馈,当您放置 %{HTTP_HOST} 时,内部端口会引起无限循环。
如果有人有更聪明的方法来获取不带端口的 %{HTTP_HOST}。这对下一个来说可能会很方便。