基於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
測試:
 

 
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
測試
 

 

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
測試
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章