Nginx目录别名(Alias)配置多个项目

最近很多小伙伴问我nginx如何配置多个项目这个问题,下面关于简单配合 php(项目1 需要配置解析PHP的模块) 和 html (项目2)对demo 为例: 首先找到nginx是配置文件中的server模块:#

不了解PHP的话,可以只简单配置html目录,其他语言都是同理

server {

    #访问默认的端口号
    listen 80;  
                          
    #访问的域名    
    server_name www.test.com;         
    
    #访问的项目根目录
    root /usr/local/var/www/test;     
    
    #访问的项目根目录的默认文件
    index index.html index.php;       
    

    #项目1    php_test 为根目录下的子目录【适合路由简单的】
    location /php_test {        
        alias /usr/local/var/www/test/php;#根目录        
        index index.html index.php;    
    }

    #项目2    html 为根目录下的子目录
    location /html {        
        alias /usr/local/var/www/html;#根目录        
        index index.html;    
    }

    #项目1 【解析PHP】    
    location ~ /php_test/.+\.php.*$ {        
    if ($fastcgi_script_name ~ /php_test/(.+\.php.*)$) {           
        set $valid_fastcgi_script_name $1;#访问匹配的文件        
    }        
        fastcgi_pass  127.0.0.1:9000;        
        fastcgi_index  index.php;        
        fastcgi_param  SCRIPT_FILENAME /usr/local/var/www/test/php/$valid_fastcgi_script_name;        
        include  fastcgi_params;    
    }

    #项目1 【解析PHP】    
    location ~ .+\.php.*$ {        
        fastcgi_pass  127.0.0.1:9000;        
        fastcgi_index  index.php;        
        fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;        
        fastcgi_param  SCRIPT_FILENAME  $request_filename;        
        include  fastcgi_params;    
     }
     
         #伪静态的方法【适合路由比较复杂的】
         location ^~ /php_test {
            rewrite /php_test/(.*)$ /php_test/index.php?=$1 last; break;
            location ~ \.php$ {   
              fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/tmp/php-cgi-73.sock;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME /usr/local/var/www/test/php_test/index.php; 
            }
        }


}

访问根目录图示:#

访问html目录:#

访问php的目录:#

上面就是关于 Nginx 的 Server 模块的多目录的简单配置~

作者:Carver-大脸猫

出处:https://www.cnblogs.com/carver/articles/17115772.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

转载请注明原处

posted @   Carver-大脸猫  阅读(1218)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up light_mode palette
选择主题
menu