繁体   English   中英

Nginx 反向代理到 Apache2 - 不起作用 - 重定向太多

[英]Nginx reverse proxy to Apache2 - not working - too many redirects

一开始我想道歉,但我是 Nginx 的新手。

我有一个 VPS,其中有一些 PHP/Symfony 项目(托管在 Apache2 中)。 现在我需要向 VPS 添加一个新应用程序(在 Vue 中,尤其是 NuxtJS)。

所以我决定用 Apache2 的反向代理设置 Nginx。 我的想法是在 Nginx 上可以运行 NuxtJS 应用程序,而 PHP 应用程序仍然可以在 Apache2 上。

我遵循了本教程: https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-one-ubuntu -18-04-服务器

  • 首先,我更改了/etc/apache2/ports.conf文件。 听 80 到听 8080 并删除 443 SSL 选项。 在这个文件中现在只有Listen 8080
  • 然后我更改了/etc/apache/sites-available中的配置。 我将 PHP 项目的现有配置编辑为:
  1. 删除 SSL 配置
  2. 编辑www.domain.com.confdomain.com.conf -> 将 *80 重写为 *8080
  3. apache2 域上的 sudo a2dissite
  • 接下来我安装了 Nginx,在/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文件。
  • 将 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选项中,我尝试了localhost127.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM