Nginx

Nginx:域的proxy_pass路徑到不同的Web伺服器

  • February 21, 2022

我們計劃將我們的 Web 應用程序從基於 php 本機的應用程序升級到基於 PHP 框架 (Laravel) 以增強應用程序的安全性和性能。我的任務是拆分流量,其中每個請求都指向app.localhost沒有後綴的域,/v3仍然轉發到 Web 伺服器節點上的舊應用程序,並使用到 Web 伺服器節點的路徑php-native代理所有請求。下面是我的配置,導致 Laravel 生成的所有資產(css 和 js)和 URL 都指向根路徑。/v3``laravel

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/v3指向laravelWeb 伺服器?

我已經找到了我的解決方案。

第 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/;
   }
}

Step2:覆蓋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-issues-44572

引用自:https://serverfault.com/questions/1094154