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
- 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;
- }
- }
- }
分别将 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;
- }
- }
来源: http://www.bubuko.com/infodetail-2740034.html