码迷,mamicode.com
首页 > 其他好文 > 详细

nginx配置及其虚拟主机的搭建

时间:2018-08-24 11:42:57      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:nss   events   bin   fips   Opens   listen   proc   修改   usr   

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配置及其虚拟主机的搭建

标签:nss   events   bin   fips   Opens   listen   proc   修改   usr   

原文地址:http://blog.51cto.com/13747009/2163731

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!