admin管理员组

文章数量:1631673

一、配置虚拟目录:
进入配置文件nginx.conf的server 配置
    1、使用alias配置虚拟目录:
     location /rest {    //匹配的path目录rest不需要真实存在alias指定的目录中
            alias /opt/TRS/www/;     //后面的"/"符号一定要带上
            index index.php index.html index.htm;
            autoindex on;      //可以打开浏览目录的权限。是off的话就是禁用,
                                只能浏览www文件中的html等文件。
        }
    最终访问的是/opt/TRS/www/目录下的文件
    如果/opt/TRS/www/目录下有index开头的html等文件,只会访问该文件,其他目录访问不到
    
    2、使用root配置虚拟目录:
     location /www {  //匹配的path目录www一定要真是存在root指定的目录中(一定要有目录)
            root /opt/TRS/;        //后面的"/"符号可有可无
            index index.php index.html index.htm;
            autoindex on;
        }
    最终访问的是/opt/TRS/www/目录下的文件
    如果/opt/TRS/www/目录下有index开头的html等文件,只会访问该文件,其他目录访问不到
浏览器访问地址:192.168.29.128:88/rest    192.168.29.128:88/www

参考网址:
http://www.nginx/doc/index.html
https://wwwblogs/kevingrace/p/6187482.html

    
    
    

二、配置虚拟主机(基于端口、ip、域名)
1、在主配置文件的倒数第二行插入以下内容:
vim /opt/TRS/nginx/conf/nginx.conf
include /opt/TRS/nginx/conf.d/*.conf;
2、在nginx文件目录下建立文件conf.d文件,用于存放配置虚拟主机的配置文件

    基于域名的虚拟主机:
1、在conf.d创建vhostsname.conf的配置文件,添加一下内容:
server {
    listen       88;    //浏览器访问的端口
    server_name  www.test1;   //浏览器访问的地址

        location / {       
            root /opt/TRS/www/;  //访问的物理目录
            index index.php index.html index.htm;
            autoindex on;      //开启访问目录权限,否则只会访问以html为尾的文件
        }
}

server {
    listen       88;
    server_name  www.test2;

        location / {
            root /opt/TRS/www/test2/;
            index index.php index.html index.htm;
            autoindex on;
        }
}

2、在电脑本机上修改hosts文件,在文件最后添加以下内容:
ip www.tset1
ip www.tset1
3、重启nginx
4、浏览器访问:www.test1:88    www.test2:88


    基于端口的虚拟主机:
1、在conf.d创建vhostsport.conf的配置文件,添加一下内容:
server {
    listen       881;
    server_name  localhsot;  

        location / {
            root /opt/TRS/www/;
            index index.php index.html index.htm;
            autoindex on;
        }
}

server {
    listen       882;
    server_name  localhsot;

        location / {
            root /opt/TRS/www/test2/;
            index index.php index.html index.htm;
            autoindex on;
        }
}

2、保存文件并重启nginx服务
3、浏览器访问:ip:881   ip:881


    基于ip的虚拟主机:
1、在conf.d创建vhostsip.conf的配置文件,添加一下内容:
server {
    listen       88;    //端口自选,不写默认80
    server_name  192.168.29.129;       //浏览器访问的地址
   
        location / {
            root /opt/TRS/www/;           //实际访问的物理路径
            index index.php index.html index.htm;
            autoindex on;
        }
}

server {
    listen       88;
    server_name  192.168.29.130;

        location / {
            root /opt/TRS/www/test2/;
            index index.php index.html index.htm;
            autoindex on;
        }
}

2、把新添加的ip绑定到linux的网卡中,否侧出错(linux重启后绑定的网卡失效)
ip addr add 192.168.29.129 dev ens33
ip addr add 192.168.29.130 dev ens33
查看:ip add

3、重启nginx服务
4、浏览器访问:192.168.29.129:88  192.168.29.130:88

参考网址:
https://wwwblogs/fps2tao/p/9958009.html
https://blog.csdn/baikeliang/article/details/72677687

本文标签: 虚拟主机虚拟目录Nginx