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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx配置及其虚拟主机的搭建相关的知识,希望对你有一定的参考价值。

1:基本配置

[[email protected] 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
[[email protected] nginx]$ ./sbin/nginx -v
nginx version: nginx/
[[email protected] 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;
        }
     }
}
[[email protected] nginx]$ tail -n1 /etc/hosts
172.25.44.18   www.dpq.com
[[email protected] nginx]$ cat ./html/www/index.html 
www.dpq.com
[[email protected] 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配置及其虚拟主机的搭建的主要内容,如果未能解决你的问题,请参考以下文章

Nginx的配置及其虚拟主机应用

在虚拟机上用nginx怎么搭建lnmp

Nginx配置多个基于域名的虚拟主机+实验环境搭建+测试

Nginx配置多个基于域名的虚拟主机+实验环境搭建+测试

13_搭建Nginx服务器配置网页认证基于域名的虚拟主机ssl虚拟主机

基于Nginx搭建Web服务器及虚拟主机相关配置详解