1. 程式人生 > >nginx配置及其虛擬主機的搭建

nginx配置及其虛擬主機的搭建

添加 eve server sbin work pen processes onf host

1:基本配置
[dev@HC-25-44-18 nginx]$ ./sbin/nginx -V
nginx version: nginx/
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
[dev@HC-25-44-18 nginx]$ ./sbin/nginx -v
nginx version: nginx/
[dev@HC-25-44-18 nginx]$ cat ./conf/nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}

http {
     include mime.types;
     default_type application/octet-stream;
     sendfile on;
     keepalive_timeout 65;
     server {
        listen 80;
        server_name www.dpq.com;
        location / {
            root html/www;
            index index.html index.htm;
        }
     }
}
[dev@HC-25-44-18 nginx]$ tail -n1 /etc/hosts
172.25.44.18   www.dpq.com
[dev@HC-25-44-18 nginx]$ cat ./html/www/index.html 
www.dpq.com
[dev@HC-25-44-18 nginx]$ curl www.dpq.com
www.dpq.com

2:基於域名的虛擬主機

-bash-4.1# cat conf/nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}

http {
     include mime.types;
     default_type application/octet-stream;
     sendfile on;
     keepalive_timeout 65;
     server {
        listen 80;
        server_name www.dpq.com;
        location / {
            root html/www;
            index index.html index.htm;
        }
     }
     server {
        listen 80;
        server_name bbs.dpq.com;
        location / {
            root html/bbs;
            index index.html index.htm;
        }
     }
     server {
        listen 80;
        server_name test.dpq.com;
        location / {
            root html/test;
            index index.html index.htm;
        }
     }
}
##################################
-bash-4.1# tree html/
html/
├── 50x.html
├── bbs
│?? └── index.html
├── index.html
├── test
│?? └── index.html
└── www
    └── index.html

3:基於端口的虛擬主機

-bash-4.1# curl www.dpq.com
www.dpq.com
-bash-4.1# curl www.dpq.com:80
www.dpq.com
-bash-4.1# curl www.dpq.com:81
bbs.dpq.com
-bash-4.1# curl www.dpq.com:82
test.dpq.com
-bash-4.1# cat conf/nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}

http {
     include mime.types;
     default_type application/octet-stream;
     sendfile on;
     keepalive_timeout 65;
     server {
        listen 80;
        server_name www.dpq.com;
        location / {
            root html/www;
            index index.html index.htm;
        }
     }
     server {
        listen 81;
        server_name www.dpq.com;
        location / {
            root html/bbs;
            index index.html index.htm;
        }
     }
     server {
        listen 82;
        server_name www.dpq.com;
        location / {
            root html/test;
            index index.html index.htm;
        }
     }
}

4:基於ip的虛擬主機

  • 添加ip
    ip addr add 10.0.0.1/24 dev eth0
    ip addr add 10.0.0.2/24 dev eth0
  • 修改配置文件
 bash-4.1# cat conf/nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}

http {
     include mime.types;
     default_type application/octet-stream;
     sendfile on;
     keepalive_timeout 65;
     server {
        listen 172.25.44.18:80;
        server_name www.dpq.com;
        location / {
            root html/www;
            index index.html index.htm;
        }
     }
     server {
        listen 10.0.0.1:81;
        server_name www.dpq.com;
        location / {
            root html/bbs;
            index index.html index.htm;
        }
     }
     server {
        listen 10.0.0.2:82;
        server_name www.dpq.com;
        location / {
            root html/test;
            index index.html index.htm;
        }
     }
}
  • 測試

-bash-4.1# curl 172.25.44.18:80
www.dpq.com
-bash-4.1# curl 10.0.0.1:81
bbs.dpq.com
-bash-4.1# curl 10.0.0.2:82
test.dpq.com

5:虛擬主機的單獨配置

  • nginx修改前配置
-bash-4.1# cat -n nginx.conf
     1  worker_processes  1;
     2  
     3  events {
     4      worker_connections  1024;
     5  }
     6  
     7  http {
     8       include mime.types;
     9       default_type application/octet-stream;
    10       sendfile on;
    11       keepalive_timeout 65;
    12       server {
    13          listen 80;
    14          server_name www.dpq.com;
    15          location / {
    16              root html/www;
    17              index index.html index.htm;
    18          }
    19       }
    20       server {
    21          listen 80;
    22          server_name bbs.dpq.com;
    23          location / {
    24              root html/bbs;
    25              index index.html index.htm;
    26          }
    27       }
    28       server {
    29          listen 80;
    30          server_name test.dpq.com;
    31          location / {
    32              root html/test;
    33              index index.html index.htm;
    34          }
    35       }
    36  }
  • 分別將server塊取出來
-bash-4.1# sed -n ‘28,35p‘ nginx.conf > test/test.conf
-bash-4.1# sed -n ‘20,27p‘ nginx.conf > test/bbs.conf
-bash-4.1# sed -n ‘12,19p‘ nginx.conf > test/www.conf
-bash-4.1# sed -i ‘12,35d‘ nginx.conf      ## 刪除
  • 修改後的配置
 -bash-4.1# cat ../conf/nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}

http {
     include mime.types;
     default_type application/octet-stream;
     sendfile on;
     keepalive_timeout 65;
     include  test/*;
}
-bash-4.1# cat ../conf/test/www.conf 
     server {
        listen 80;
        server_name www.dpq.com;
        location / {
            root html/www;
            index index.html index.htm;
        }
     }
-bash-4.1# cat ../conf/test/bbs.conf 
     server {
        listen 80;
        server_name bbs.dpq.com;
        location / {
            root html/bbs;
            index index.html index.htm;
        }
     }
-bash-4.1# cat ../conf/test/test.conf 
     server {
        listen 80;
        server_name test.dpq.com;
        location / {
            root html/test;
            index index.html index.htm;
        }
     }

nginx配置及其虛擬主機的搭建