AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1094154
Accepted
Abu Dawud
Abu Dawud
Asked: 2022-02-19 17:57:37 +0800 CST2022-02-19 17:57:37 +0800 CST 2022-02-19 17:57:37 +0800 CST

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

  • 772

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

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 服务器?

nginx laravel
  • 1 1 个回答
  • 445 Views

1 个回答

  • Voted
  1. Best Answer
    Abu Dawud
    2022-02-21T17:28:46+08:002022-02-21T17:28:46+08:00

    我已经找到了我的解决方案。

    第 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

    • 0

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve