![](/img/trans.png)
[英]Nginx as reverse proxy for Apache (RHEL 8), http to https rewrites, ERR_TOO_MANY_REDIRECTS
[英]Nginx reverse proxy to Apache2 - not working - too many redirects
一开始我想道歉,但我是 Nginx 的新手。
我有一个 VPS,其中有一些 PHP/Symfony 项目(托管在 Apache2 中)。 现在我需要向 VPS 添加一个新应用程序(在 Vue 中,尤其是 NuxtJS)。
所以我决定用 Apache2 的反向代理设置 Nginx。 我的想法是在 Nginx 上可以运行 NuxtJS 应用程序,而 PHP 应用程序仍然可以在 Apache2 上。
/etc/apache2/ports.conf
文件。 听 80 到听 8080 并删除 443 SSL 选项。 在这个文件中现在只有Listen 8080
。/etc/apache/sites-available
中的配置。 我将 PHP 项目的现有配置编辑为:www.domain.com.conf
和domain.com.conf
-> 将 *80 重写为 *8080/etc/nginx/nginx.conf
中取消注释 gzip 选项并添加proxy_cache_path /var/cache levels=1:2 keys_zone=reverse_cache:60m inactive=90m max_size=1000m;
/etc/nginx/sites-available
中的default
文件,并在同一目录中创建了apache
文件。ln -s apache /etc/nginx/sites-enabled
链接。sudo iptables -I INPUT -p tcp --dport 8080 ! -s your_server_ip -j REJECT --reject-with tcp-reset
sudo iptables -I INPUT -p tcp --dport 8080 ! -s your_server_ip -j REJECT --reject-with tcp-reset
Nginx 配置文件Apache
看起来像:
server {
server_name my_domain.cz www.my_domain.cz;
root /var/www/my_domain/web;
index index.php app.php index.htm index.html;
location / {
try_files $uri $uri/ /app.php;
}
location ~ \.php$ {
proxy_pass http://localhost:8080;
proxy_set_header Host $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 $scheme;
}
location ~ /\.ht {
deny all;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/my_domain.cz/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/my_domain.cz/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
}
server {
server_name adminer.my_domain.cz www.adminer.my_domain.cz;
root /var/www/adminer;
index index.php index.htm index.html;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
proxy_pass http://localhost:8080;
proxy_set_header Host $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 $scheme;
}
location ~ /\.ht {
deny all;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/adminer.my_domain.cz/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/adminer.my_domain.cz/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
}
在proxy_pass
选项中,我尝试了localhost
或127.0.0.1
或我的服务器公共 IP。 这些选项中的任何一个都不起作用。
Nginx 正在运行( sudo nginx -t
可以),Apache2 也是。 当我尝试访问我的网站时,我得到了ERR_TOO_MANY_REDIRECTS
。 生无可恋。 我发现的建议都没有奏效。
ERR_TOO_MANY_REDIRECTS
错误基本上意味着您有一个重定向循环。 由于您正在重新配置服务器,因此可能有多种原因导致循环。 首先,只需确保删除所有 cookies 并缓存在浏览器中,如果有的话,缓存在服务器上。
If that is not solving the loop, it might be caused by a redirect to https in a .htaccess
or apache config file on your apache php server.
如今,大多数服务器都配置为将所有流量从 http 重定向到 https。 在.htaccess
文件中,您可能会发现如下内容:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
基本上,这意味着所有不是 https 的请求都将被重定向到 https。
Since you have reconfigured the apache server to http and all requests from the Nginx reverse proxy will be http, the server will send a redirect request (301) to the browser. The browser will send a https request to Nginx, which forwards it as a http request to Apache, which sends a redirect to https to the browser. 这将是一个无限循环。 浏览器识别出这个循环并抛出ERR_TOO_MANY_REDIRECTS
。
在.htaccess
或 apache 虚拟主机配置中删除到 https 的重定向可能会解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.