nginx如何配置二级目录访问另一台机器上apache代理的原生php项目

运行环境

虚拟机 hostA: centos7 + nginx
本机电脑 hostB:windows + html + apache

虚拟机A的 ip 是 x.x.x.222
本机电脑B的ip是 x.x.x.111

详细环境

虚拟机A中做了如下操作

  1. 修改host

     127.0.0.1 hostB 
     x.x.x.111 hostA
  2. nginx 的配置文件路径是 /etc/nginx/nginx.conf

  3. 在配置文件的server块中添加了如下配置

     location /aaa/ {
         proxy_pass hostB;
     }

本机电脑B做了如下操作

  1. 修改host文件
     127.0.0.1 hostB
     x.x.x.222 hostA
  2. apache 的配置文件未改变,默认执行的 web 目录为 xxxxx/xampp/htdocs
  3. web 目录下添加了一个 index.html 文件,index.html 内容如下
     .
     .
     .
     <body>
         <h1>hello1</h1>
         <a href="/hello">hello</a>
     </body>
     .
     .
     .

我遇到的问题

在浏览器中访问 hostA/aaa 得到的页面和 index.html的内容一致。
打开开发者工具,鼠标悬浮在a标签上出现一个提示框,提示框内容是 hostA/hello

我的理想结果

鼠标悬浮在a标签的地址是 hostA/aaa/hello

我的问题

遇到上述情况我应该怎么配置nginx呢?

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
九霄道长
最佳答案

例子:


#PROXY-START/test

location ^~ /test
{
    proxy_pass https://www.runoob.com/;
    proxy_set_header Host www.runoob.com;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    # proxy_hide_header Upgrade;

    add_header X-Cache $upstream_cache_status;

    #Set Nginx Cache

    proxy_set_header Accept-Encoding "";
 sub_filter "菜鸟" "老鸟";
    sub_filter_once off;


    set $static_fileZRUWlvmB 0;
    if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
    {
     set $static_fileZRUWlvmB 1;
     expires 12h;
        }
    if ( $static_fileZRUWlvmB = 0 )
    {
    add_header Cache-Control no-cache;
    }
}

#PROXY-END/test
2年前 评论
讨论数量: 2

我现在的解决办法如下

  1. 在本机电脑B的web目录下添加一个目录,目录名就是aaa
  2. 将原来在 web目录下的 index.html移动到 web目录/aaa 目录下
  3. 在虚拟机A的 nginx 配置文件的 server 块中修改如下配置
    location /aaa/ {
         proxy_pass hostB/aaa/;
    }
  4. 重启虚拟机A的 nginx
  5. 再次访问 hostA/aaa 得到的 index.html,打开开发者工具,鼠标悬浮在a标签上,提示框的内容就是 hostA/aaa/hello
2年前 评论
九霄道长

例子:


#PROXY-START/test

location ^~ /test
{
    proxy_pass https://www.runoob.com/;
    proxy_set_header Host www.runoob.com;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    # proxy_hide_header Upgrade;

    add_header X-Cache $upstream_cache_status;

    #Set Nginx Cache

    proxy_set_header Accept-Encoding "";
 sub_filter "菜鸟" "老鸟";
    sub_filter_once off;


    set $static_fileZRUWlvmB 0;
    if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
    {
     set $static_fileZRUWlvmB 1;
     expires 12h;
        }
    if ( $static_fileZRUWlvmB = 0 )
    {
    add_header Cache-Control no-cache;
    }
}

#PROXY-END/test
2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!