基于nginx的虚拟主机的配置

安装pcre

tar -xvf pcre-8.32.tar.gz
cd pcre-8.32
./configure
make;make install
安装nginx
首先创建一个nginx用户,以nginx用户来运行nginx
useradd nginx
tar -xvf nginx-1.2.7.tar.gz
cd nginx-1.2.7
./configure --user=nginx --group=nginx
--prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module
make;make install
 
启动nginx时提示:error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
 
解决方法:
 
32位系统 [root@sever lib]# ln -s /usr/local/lib/libpcre.so.1 /lib
64位系统 [root@sever lib]# ln -s /usr/local/lib/libpcre.so.1 /lib64
 
启动时提示:nginx: [emerg] getpwnam("nginx --group=nginx") failed
 
解决方法:
 
把nginx配置文件中的user改为编译时指定的用户即可
 
1.配置基于主机名的虚拟主机
vi /etc/hosts
192.168.183.138 www.test.com
192.168.183.138 www.a.org
echo "www.test.com" > /web/test/index.html
echo "www.a.org" > /web/test/index.html
修改nginx的配置文件,添加两段主机配置
server {
        listen 80;
        server_name www.test.com;
        location / {
                root    /web/test/;
                index   index.html index.htm;
        }
}
 server {
        listen 80;
        server_name www.a.org;
        location / {
                root    /web/a/;
                index   index.html index.htm;
        }
}
root用来指定网页根目录
配置文件检测
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful
启动nginx /usr/local/nginx/sbin/nginx -s reopen
 
如果报错:nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed
解决方法:执行 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
 
2.基于端口的虚拟主机配置
修改nginx的配置文件
server {
        listen 9090;
        location / {
                root    /web/test/;
                index   index.html index.htm;
        }
}
 
server {
        listen 8080;
        location / {
                root    /web/a/;
                index   index.html index.htm;
        }
}
 
配置文件检测
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful
重启nginx /usr/local/nginx/sbin/nginx -s reload
 
3.配置基于IP地址的虚拟主机
ifconfig eth0:0 192.168.183.139/24
这里我本机的IP地址是192.168.183.138
修改配置文件
server {
        listen 192.168.183.138:80;
        server_name www.test.com;
        location / {
                root    /web/test/;
                index   index.html index.htm;
        }
}
 server {
        listen 192.168.183.139:80;
       server_name www.a.org;
        location / {
                root    /web/a/;
                index   index.html index.htm;
        }
}
 
配置文件检测
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful
启动nginx /usr/local/nginx/sbin/nginx
原文地址:https://www.cnblogs.com/rnckty/p/4310557.html