Nginx:域名到不同 Web 服务器的 proxy_pass 路径

Nginx:域名到不同 Web 服务器的 proxy_pass 路径

我们计划将我们的 Web 应用程序从基于 php 本机的应用程序升级到基于 PHP 框架 (Laravel) 的应用程序,以增强应用程序的安全性和性能。我的任务是拆分流量,其中每个指向app.localhost没有后缀的域的请求/v3仍转发到php-nativeWeb 服务器节点上的旧应用程序,并将所有请求与Web 服务器节点/v3的路径代理laravel。以下是我的配置,导致 Laravel 生成的所有资产 (css 和 js) 和 URL 指向根路径。

Laravel 生成的 URL 指向旧应用程序

前端代理(公共网络)

server {
    listen       80;
    listen  [::]:80;
    server_name  app.localhost;

    # PHP Native APP
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://php-native/;
    }

    # Laravel (APP v3)
    location /v3/ {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://laravel/;
    }
}

Web 服务器(专用网络)

php-native网络服务器

server {
    listen       80;
    listen  [::]:80;
    server_name  app.localhost;

    root   /usr/share/nginx/html/webapp/app;

    location / {
        index index.php index.html index.htm;

        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass   php56-fpm:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /var/www/html/webapp/app/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

laravel网络服务器

server {
    listen       80;
    server_name  app.localhost;

    root   /usr/share/nginx/html/webapp/app-v3/public;
    index index.php index.html index.htm;

    location / {
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass   php74-fpm:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /var/www/html/webapp/app-v3/public/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

谢谢

更新

我的问题是:如何分割流量,以便任何指向的请求app.localhost仍然转发到php-nativeWeb 服务器,并且所有指向app.localhost/v3Weblaravel服务器的请求?

答案1

我已经找到了解决方案。

步骤 1:添加X-Frowarded-Prefix到前端代理

server {
    listen       80;
    listen  [::]:80;
    server_name  app.localhost;

    # PHP Native APP
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://php-native/;
    }

    # Laravel (APP v3)
    location /v3/ {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Prefix "/v3";
        proxy_pass http://laravel/;
    }
}

步骤2:覆盖getTrustedHeaderNames函数

protected function getTrustedHeaderNames()
{
    switch ($this->headers) {
        case 'HEADER_X_FORWARDED_AWS_ELB':
        case Request::HEADER_X_FORWARDED_AWS_ELB:
            return Request::HEADER_X_FORWARDED_AWS_ELB;

        case 'HEADER_FORWARDED':
        case Request::HEADER_FORWARDED:
            return Request::HEADER_FORWARDED;

        case 'HEADER_X_FORWARDED_FOR':
        case Request::HEADER_X_FORWARDED_FOR:
            return Request::HEADER_X_FORWARDED_FOR;

        case 'HEADER_X_FORWARDED_HOST':
        case Request::HEADER_X_FORWARDED_HOST:
            return Request::HEADER_X_FORWARDED_HOST;

        case 'HEADER_X_FORWARDED_PORT':
        case Request::HEADER_X_FORWARDED_PORT:
            return Request::HEADER_X_FORWARDED_PORT;

        case 'HEADER_X_FORWARDED_PROTO':
        case Request::HEADER_X_FORWARDED_PROTO:
            return Request::HEADER_X_FORWARDED_PROTO;

        // add this to support x-forwarded-prefix
        case 'HEADER_X_FORWARDED_PREFIX':
        case Request::HEADER_X_FORWARDED_PREFIX:
            return Request::HEADER_X_FORWARDED_PREFIX;

        // add | Request::HEADER_X_FORWARDED_PREFIX
        default:
            return Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO | Request::HEADER_X_FORWARDED_AWS_ELB;
    }

    return $this->headers;
}

这是基于 Symfony 的问题symfony-问题-44572

相关内容