nginx反向代理测试

测试的文件都放在了附件中,下载附件打开test.html查看

以下nginx代理配置均自己测试通过

测试内容为:

访问http://127.0.0.1:8011/p/时,实际访问的是http://127.0.0.1:8010/

访问http://127.0.0.1:8011/p/pl/时,实际访问的是http://127.0.0.1:8010/a/

相关配置如下:

8011的nginx配置:

server {
    listen        8011;
    server_name  t.local;
    root   "E:/Software/phpstudy_pro/WWW/t";
    location / {
        index index.php index.html error/index.html;
        error_page 400 /error/400.html;
        error_page 403 /error/403.html;
        error_page 404 /error/404.html;
        error_page 500 /error/500.html;
        error_page 501 /error/501.html;
        error_page 502 /error/502.html;
        error_page 503 /error/503.html;
        error_page 504 /error/504.html;
        error_page 505 /error/505.html;
        error_page 506 /error/506.html;
        error_page 507 /error/507.html;
        error_page 509 /error/509.html;
        error_page 510 /error/510.html;
        include E:/Software/phpstudy_pro/WWW/t/nginx.htaccess;
        autoindex  off;
    }

    location ~ ^/p/(.*)$ {  
        proxy_pass http://127.0.0.1:8010/$1;  
        proxy_set_header Host $host;  
        proxy_set_header X-Real-IP $remote_addr;  
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
        # 这里的$1是一个正则表达式捕获的变量,代表/p/后面的所有内容  
    }

  
    # 如果需要的话,可以添加额外的location来处理特定文件类型  
    # 例如:  
    # location ~ \.php$ {  
    #     # PHP文件的代理设置  
    #     proxy_pass http://127.0.0.1:8080;  
    #     fastcgi_pass 127.0.0.1:9000;  
    #     fastcgi_index index.php;  
    #     include fastcgi_params;  
    # }  
  
    # 如果需要处理静态文件,例如图片  
    # location ~ ^/p/\.(jpeg|jpg|png|gif)$ {
    #     proxy_pass http://127.0.0.1:8080/$1;  
    # }

    # 顺序不能和location ~ ^/p/(.*)$颠倒,否则匹配后不走到下面
    location ~ \.php(.*)$ {
        fastcgi_pass   127.0.0.1:9006;
        fastcgi_index  index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include        fastcgi_params;
    }

}

8010的nginx配置:

server {
    listen        8010;
    server_name  p.local;
    root   "E:/Software/phpstudy_pro/WWW/p";

    location / {
        index index.php index.html error/index.html;
        error_page 400 /error/400.html;
        error_page 403 /error/403.html;
        error_page 404 /error/404.html;
        error_page 500 /error/500.html;
        error_page 501 /error/501.html;
        error_page 502 /error/502.html;
        error_page 503 /error/503.html;
        error_page 504 /error/504.html;
        error_page 505 /error/505.html;
        error_page 506 /error/506.html;
        error_page 507 /error/507.html;
        error_page 509 /error/509.html;
        error_page 510 /error/510.html;
        include E:/Software/phpstudy_pro/WWW/p/nginx.htaccess;
        autoindex  on;
    }

    location ~ \.php(.*)$ {
        fastcgi_pass   127.0.0.1:9006;
        fastcgi_index  index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include        fastcgi_params;
    }

    location /pl/ {
        # 尝试列出目录内容
        # autoindex on;
        # 指定目录的根路径
        alias "E:/Software/phpstudy_pro/WWW/p/a/";
        # 禁用缓存
        # add_header Cache-Control no-store,no-cache,must-revalidate,proxy-revalidate;
        # add_header Pragma no-cache;
        # add_header Expires 0;
        
        # 代理到后端PHP-FPM服务器
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9006; # PHP-FPM默认监听9000端口
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;

            # 处理No input file specified错误
            fastcgi_param  DOCUMENT_ROOT    $document_root;
            fastcgi_param  SCRIPT_FILENAME  $request_filename;
        }  

        # 其他请求直接代理到/a/
        proxy_pass http://127.0.0.1:8010/a/;
        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 / {
    #     try_files $uri $uri/ /index.php$is_args$args;
    # }
    # location ~ \.php(.*)$ {
    #     fastcgi_pass 127.0.0.1:9006;
    #     fastcgi_index index.php;
    #     include fastcgi_params;
    #     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #     # include fastcgi.conf;
    # }

}
 

可实现使用nginx配置代理,访问一个目录或域名时实际访问到另一个目录或域名

  • 16
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值