nginx配置说明及虚拟主机站点的配置(基于域名)

nginx配置说明及虚拟主机站点的配置
1.nginx目录说明:
/application/nginx
├── client_body_temp
├── conf #这是nginx所有的配置文件的目录,极其重要
│   ├── fastcgi.conf #fastcgi配置文件
│   ├── fastcgi.conf.default
│   ├── fastcgi_params #fastcgi的参数文件
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types
│   ├── mime.types.default
│   ├── nginx.conf #nginx默认的主配置文件
│   ├── nginx.conf.default
│   ├── scgi_params
│   ├── scgi_params.default
│   ├── uwsgi_params
│   ├── uwsgi_params.default
│   └── win-utf
├── fastcgi_temp #临时目录
├── html #这是编译安装是nginx的默认站点目录,类似apache的htdocs目录。
│   ├── 50x.html #错误页面优雅显示文件,例如出现502错误调用此页面
│   ├── index.html #默认首页文件,实际环境中,大家习惯用index.html、index.php、index.php。
├── logs #这是nginx默认的日志目录
│   ├── access.log #访问日志
│   ├── error.log #错误日志
│   └── nginx.pid #nginx.pid文件,nginx进程启动后,会把所有进程ID号写到此文件。
├── proxy_temp
├── sbin #ningx命令目录。
│   └── nginx #nginx命令nginx
├── scgi_temp #临时目录
└── uwsgi_temp #临时目录


2.nginx配置文件详解。
#备份与配置文件相同
[root@backup conf]# diff nginx.conf nginx.conf.default
#取消注释
[root@backup conf]# egrep -v "#|^$" 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  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@backup conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf


4.配置nginx虚拟主机实践。
4.1先更改主配置文件相关配置。
[root@backup conf]# cat nginx.conf
worker_processes  8;
user nginx nginx;
events {
   use epoll;
    worker_connections  20480;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.etiantian.org etiantian.org;
        location / {
            root   html;
            index  index.html index.htm;
            }
     }
}
[root@backup conf]# ../sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
4.2开始配置基于域名的虚拟主机。
4.2.1创建站点目录
[root@backup ~]# mkdir /data0/www/{www,bbs,blog} -p
[root@backup ~]# for n in www blog bbs;do echo "$n" >/data0/www/$n/index.html;done
[root@backup ~]# tree /data0/www/
/data0/www/
├── bbs
│   └── index.html
├── blog
│   └── index.html
└── www
    └── index.html
授权、创建日志目录
[root@backup ~]# chown -R nginx.nginx /data0/www
[root@backup ~]# mkdir /app/logs -p
4.2.2修改配置文件。
[root@backup ~]# echo www >/data0/www/www/index.html
(修改了站点目录)
[root@backup ~]# cat /application/nginx/conf/nginx.conf
worker_processes  8;
user nginx nginx;
events {
   use epoll;
    worker_connections  20480;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.etiantian.org etiantian.org;
        location / {
            root   /data0/www/www;
            index  index.html index.htm;
            }
     }
}
#重启nginx。
[root@backup ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@backup ~]# /application/nginx/sbin/nginx -s reload
[root@backup ~]# netstat -lntup|grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      14664/nginx         
[root@backup ~]# lsof -i:80
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   14664  root    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22139 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22140 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22141 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22142 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22143 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22144 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22145 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22146 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
[root@backup ~]# ps -ef|grep nginx
root     14664     1  0 13:46 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx
nginx    22139 14664  0 14:49 ?        00:00:00 nginx: worker process        
nginx    22140 14664  0 14:49 ?        00:00:00 nginx: worker process        
nginx    22141 14664  0 14:49 ?        00:00:00 nginx: worker process        
nginx    22142 14664  0 14:49 ?        00:00:00 nginx: worker process        
nginx    22143 14664  0 14:49 ?        00:00:00 nginx: worker process        
nginx    22144 14664  0 14:49 ?        00:00:00 nginx: worker process        
nginx    22145 14664  0 14:49 ?        00:00:00 nginx: worker process        
nginx    22146 14664  0 14:49 ?        00:00:00 nginx: worker process        
root     22151  4544  0 14:49 pts/0    00:00:00 grep nginx
4.3 测试
编辑WINDOWS 7 的hosts配置。C:\Windows\System32\drivers\etc\hosts
192.168.0.251 www.etiantian.org blog.etiantian.org bbs.etiantian.org etiantian.org
在浏览器端进行检查http://www.etiantian.org/。
www    <--测试OK


4.4新增bbs和blog站点。
修改配置文件,如下:
[root@backup ~]# cat /data0/www/blog/index.html 
blog
[root@backup ~]# cat /application/nginx/conf/nginx.conf
worker_processes  8;
user nginx nginx;
events {
   use epoll;
    worker_connections  20480;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.etiantian.org etiantian.org;
        location / {
            root   /data0/www/www;
            index  index.html index.htm;
            }
     }
   ###BBS
      server {
        listen       80;
        server_name  bbs.etiantian.org;
        location / {
            root   /data0/www/bbs;
            index  index.html index.htm;
            }
     }
   ###BLOG
          server {
        listen       80;
        server_name  blog.etiantian.org;
        location / {
            root   /data0/www/blog;
            index  index.html index.htm;
            }
     }
}
#新增首页文件
[root@backup ~]# for n in bbs blog;do echo "$n" > /data0/www/$n/index.html;done
#重启nginx服务
[root@backup ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@backup ~]# /application/nginx/sbin/nginx -s reload
[root@backup ~]# lsof -i:80
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   14664  root    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22174 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22175 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22176 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22177 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22178 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22179 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22180 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22181 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
#测试
地址:http://bbs.etiantian.org/
结果:bbs
地址:http://blog.etiantian.org/
结果:blog
5.配置nginx日志
(修改配置文件,添加日志相关配置。)
[root@backup ~]# cat /application/nginx/conf/nginx.conf
worker_processes  8;
user nginx nginx;
error_log  /app/logs/nginx_error.log  crit;
events {
   use epoll;
    worker_connections  20480;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    log_format  commonlog  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    keepalive_timeout  65;
   ###WWW
    server {
        listen       80;
        server_name  www.etiantian.org etiantian.org;
        location / {
            root   /data0/www/www;
            index  index.html index.htm;
            access_log /app/logs/www_access.log commonlog;
            }
     }
   ###BBS
      server {
        listen       80;
        server_name  bbs.etiantian.org;
        location / {
            root   /data0/www/bbs;
            index  index.html index.htm;
            access_log /app/logs/bbs_access.log commonlog;
            }
     }
   ###BLOG
          server {
        listen       80;
        server_name  blog.etiantian.org;
        location / {
            root   /data0/www/blog;
            index  index.html index.htm;
            access_log /app/logs/blog_access.log commonlog;
            }
     }
}
#重启服务器,查看相关日志文件。
[root@backup ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@backup ~]# /application/nginx/sbin/nginx -s reload
[root@backup ~]# lsof -i:80
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   14664  root    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22236 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22237 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22238 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22239 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22240 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22241 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22242 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22243 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
[root@backup ~]# ll /app/logs/


使用浏览器访问三个虚拟主机站点,bbs,blog,www站点。
查看日志目录/app/logs
[root@backup ~]# ll /app/logs/
total 12
-rw-r--r-- 1 root root 329 Feb 15 15:16 bbs_access.log
-rw-r--r-- 1 root root 329 Feb 15 15:16 blog_access.log
-rw-r--r-- 1 root root   0 Feb 15 15:14 nginx_error.log
-rw-r--r-- 1 root root 329 Feb 15 15:16 www_access.log
[root@backup ~]# tail /app/logs/bbs_access.log 
192.168.0.104 - - [15/Feb/2018:15:16:48 +0800] "GET / HTTP/1.1" 200 4 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" "-"
192.168.0.104 - - [15/Feb/2018:15:16:48 +0800] "GET /favicon.ico HTTP/1.1" 404 168 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" "-"
[root@backup ~]# tail /app/logs/blog_access.log 
192.168.0.104 - - [15/Feb/2018:15:16:50 +0800] "GET / HTTP/1.1" 200 5 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" "-"
192.168.0.104 - - [15/Feb/2018:15:16:50 +0800] "GET /favicon.ico HTTP/1.1" 404 168 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" "-"
[root@backup ~]# tail /app/logs/www_access.log 
192.168.0.104 - - [15/Feb/2018:15:16:54 +0800] "GET / HTTP/1.1" 200 4 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" "-"
192.168.0.104 - - [15/Feb/2018:15:16:54 +0800] "GET /favicon.ico HTTP/1.1" 404 168 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" "-"
192.168.0.104 - - [15/Feb/2018:15:17:11 +0800] "GET / HTTP/1.1" 200 4 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" "-"
[root@backup ~]# tail /app/logs/nginx_error.log 


6.虚拟主机配置与主配置文件nginx.conf分开存放。
[root@backup ~]# cp /application/nginx/conf/nginx.conf /application/nginx/conf/extra/nginx-vhosts.conf
(具体配置见补充)
[root@backup ~]# vi /application/nginx/conf/nginx.conf
[root@backup ~]# vi /application/nginx/conf/extra/nginx-vhosts.conf 
[root@backup ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@backup ~]# /application/nginx/sbin/nginx -s reload
[root@backup ~]# lsof -i:80
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   14664  root    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22264 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22265 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22266 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22267 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22268 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22269 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22270 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
nginx   22271 nginx    6u  IPv4  35434      0t0  TCP *:http (LISTEN)
补充:
主配置文件/application/nginx/conf/nginx.conf的配置为:
[root@backup ~]# cat /application/nginx/conf/nginx.conf
worker_processes  8;
user nginx nginx;
error_log  /app/logs/nginx_error.log  crit;
events {
   use epoll;
    worker_connections  20480;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    log_format  commonlog  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    keepalive_timeout  65;
    include extra/nginx-vhosts.conf;
}


虚拟主机application/nginx/conf/extra/nginx-vhosts.conf的配置为:
[root@backup ~]# cat /application/nginx/conf/extra/nginx-vhosts.conf    
    ###WWW
    server {
        listen       80;
        server_name  www.etiantian.org etiantian.org;
        location / {
            root   /data0/www/www;
            index  index.html index.htm;
            access_log /app/logs/www_access.log commonlog;
            }
     }
   ###BBS
      server {
        listen       80;
        server_name  bbs.etiantian.org;
        location / {
            root   /data0/www/bbs;
            index  index.html index.htm;
            access_log /app/logs/bbs_access.log commonlog;
            }
     }
   ###BLOG
          server {
        listen       80;
        server_name  blog.etiantian.org;
        location / {
            root   /data0/www/blog;
            index  index.html index.htm;
            access_log /app/logs/blog_access.log commonlog;
            }
     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值