账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Nginx如何使用一个域名配置多个目录
    22
    0
    server {
        listen       *:80;
        server_name  admin.hiphop.top;
    
        location / {
            root   "D:\Office\PHPStudy\PHPTutorial\WWW\MyWork\VhiphopAdmin\dist";
            index index.html;
            try_files $uri $uri/ /index.html;
        }
    
        location /api {
            root   "D:\Office\PHPStudy\PHPTutorial\WWW\MyWork\VhiphopAdminApi\public";
            index  index.php;
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ \.php(.*)$  {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

    配置代码如上
    访问 admin.hiphop.top
    可以匹配到 "D:OfficePHPStudyPHPTutorialWWWMyWorkVhiphopAdmindist"

    访问 admin.hiphop.top/api
    不能匹配到 "D:OfficePHPStudyPHPTutorialWWWMyWorkVhiphopAdminApipublic"

    dist目录是我的前端静态文件, public目录是php api接口的入口目录
    请问如何配置才能做到 访问 admin.hiphop.top/* 时匹配dist目录
    并且访问 admin.hiphop.top/api 优先匹配php的public目录

    配置2个域名是没问题的 因为浏览器有跨域问题, 所以现在想只用一个域名

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 断桥ㄣ☆残膤 普通会员 1楼

      Nginx可以使用一个域名配置多个目录,这样就可以将多个不同的网站部署在同一台服务器上,以提高性能和可靠性。

      以下是一个基本的示例,说明如何在Nginx中使用一个域名配置多个目录:

      1. 首先,你需要在Nginx的配置文件(通常是nginx.conf或nginx/sites-available)中定义你的多个网站。例如:

      ``` server { listen 80; server_name example.com;

      location / {
          index index.html;
          root /var/www/example.com;
      }
      
      location /images {
          alias /var/www/example.com/images/;
      }
      
      location /uploads {
          alias /var/www/example.com/uploads/;
      }
      

      } ```

      1. 然后,你需要将这个配置文件保存为一个名为example.com sites-enabled的文件。你可以通过运行以下命令来保存这个文件:

      sudo nano /etc/nginx/sites-available/example.com

      1. 在文件的开头添加以下行:

      ``` server { listen 80; server_name example.com;

      location / {
          index index.html;
          root /var/www/example.com;
      }
      
      location /images {
          alias /var/www/example.com/images/;
      }
      
      location /uploads {
          alias /var/www/example.com/uploads/;
      }
      

      } ```

      1. 保存文件并退出。

      2. 最后,你需要重启Nginx以应用新的配置:

      sudo systemctl restart nginx

      现在,你应该可以看到你的Nginx服务器正在使用example.com网站的多个目录。每个目录都有一个特定的文件名,你可以根据需要自定义这些文件名。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部