进入nginx目录,排除temp文件后,剩余四个目录html
[root@elkzabbix01 nginx]# ls -l | grep -v templinux
总用量 36nginx
drwxr-xr-x. 2 root root 4096 7月 12 20:42 conf → 配置文件web
drwxr-xr-x. 2 root root 4096 7月 12 20:42 html → 站点信息apache
drwxr-xr-x. 2 root root 4096 7月 12 20:49 logs → 日志信息windows
drwxr-xr-x. 2 root root 4096 7月 12 20:42 sbin → 启动命令浏览器
cd /html 服务器
里面有文件index.html ,这个文件通常默认是网站的首页并发
下面进入conf文件夹,下面有一个 nginx.conf 文件app
运维有一个很重要的思想叫最小化学习,因此咱们要把配置文件简化一下:
简化命令:
egrep -v "#|^$" nginx.conf >> a.conf
^表明行首
$表明行尾
^$意思就是行首以后就是行尾,中间什么也没有,因此表明空行
worker_processes 1; 默认进程有一个
一个worker能够处理多少并发链接
events {
worker_connections 1024;
}
那么有多少个worker呢???能够看配置文件第一行
[root@elkzabbix01 conf]# ps -ef | grep nginx
root 8398 1 0 Jul12 ? 00:00:00 nginx: master process /app/zpy/nginx/sbin/nginx
zpy 8399 8398 0 Jul12 ? 00:00:00 nginx: worker process
能够看到就一个worker process ,通常认为worker process 与cpu 核数至关
因此nginx最大并发链接数是怎么计算的呢 ??
worker process 乘以 worker_connections
-----------------------
http模块
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
下面这个叫作server标签,一个server标签表明一个虚拟主机
server {
listen 80; 监听端口
server_name localhost; 域名
location / { / 表明直接在浏览器输入 http://10.0.70.3
root html; root表明nginx安装目录下的html文件夹,好比说你nginx安装在了/opt/nginx 下,那么html表明的目录就为 /opt/nginx/html,可是若是你写 root /html ,那就不是相对路径了(nginx安装目录下),那就是/html了
index index.html index.htm; 这个表明首页文件,就是说nginx下html文件夹下的index.html文件
}
error_page 500 502 503 504 /50x.html;
location = /50x.html { /50.x.html表明直接在浏览输http://10.0.70.3/50.x.html
root html;
}
关于root和alias的区别:
干脆来讲说alias标签和root标签的区别吧。
最基本的区别:alias指定的目录是准确的,root是指定目录的上级目录,而且该上级目录要含有location指定名称的同名目录。另外,根据前文所述,使用alias标签的目录块中不能使用rewrite的break。
说不明白,看下配置:
location /abc/ {
alias /home/html/abc/;
}
在这段配置下,http://test/abc/a.html就指定的是/home/html/abc/a.html。这段配置亦可改为
location /abc/ {
root /home/html/;
}
这样,nginx就会去找/home/html/目录下的abc目录了,获得的结果是相同的。
可是,若是我把alias的配置改为:
location /abc/ {
alias /home/html/def/;
}
那么nginx将会从/home/html/def/取数据,这段配置还不能直接使用root配置,若是非要配置,只有在/home/html/下创建一个 def->abc的软link(快捷方式)了。
通常状况下,在location /中配置root,在location /other中配置alias是一个好习惯。
nginx 虚拟主机的概念
所谓的虚拟主机,在web服务里就是一个独立的网站站点,这个站点对应独立的域名(也多是IP或者端口),具备独立程序及资源目录,能够独立的对外提供服务供用户访问
apache是如何定义虚拟主机的 ?
配置文件中 <VirtualHost> </VirtualHost> 内
nginx是如何定义虚拟主机的?
在配置文件中server{} 的标签
虚拟主机通常分为以下三类:
基于域名的虚拟主机
基于端口的虚拟主机
基于ip的虚拟主机
配置基于域名的虚拟主机
server {
listen 80;
server_name www.vipdailiang.com;
location / {
root html/www;
index www.html ;
}
}
1)首先须要在/app/zpy/nginx/html下建立 www目录
mkdir -p /app/zpy/nginx/html/www
2)在www目录下建立www.html,并添加www.zipeiyi.com的内容
echo "www.vipdailiang" >> /app/zpy/nginx/html/www/www.html
3)最重要的一步,nginx一旦应用在企业环境中,它的做用会变得很重要,因此修改了配置文件,不要贸然重启,须要先检查一遍nginx配置文件的合理性
/app/zpy/nginx/sbin/nginx -t
nginx: the configuration file /app/zpy/nginx-1.8.1//conf/nginx.conf syntax is ok
nginx: configuration file /app/zpy/nginx-1.8.1//conf/nginx.conf test is successful
4)平滑重启nginx
/app/zpy/nginx/sbin/nginx -s reload
备注:
若是nginx参数过多记不住怎么办??
/app/zpy/nginx/sbin/nginx -h
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /app/zpy/nginx-1.8.1//)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
最后在浏览器输入10.0.70.3
可是若是输入:www.vipdailiang.com
这是由于你电脑本机解析不到的缘由:
有两种解析的办法:
修改本机的host文件(linux是/etc/hosts ,windows的C\windows\system32\drivers\etc\hosts)
添加10.0.70.3 www.vipdailiang.com
在本地DNS里面加入域名对应关系
最后效果以下,至此基于域名的虚拟主机配置完毕
配置多个基于域名的虚拟主机
server {
listen 80;
server_name www.vipdailiang.com;
location / {
root html/www;
index www.html ;
}
}
server {
listen 80;
server_name bbs.vipdailiang.com;
location / {
root html/bbs;
index bbs.html ;
}
}
这样你就可能会想了,怎么都是80端口,那么我在浏览器输入http://10.0.70.3 会出现什么???
别急,细细道来
我作了一个实验,发现是下图的结构:
若是你用ip进行测试的化,那么应该是按照 server{}的顺序来的
总结:基与域名的虚拟主机,就不要这样测试了,应该用域名测试
到此基于域名的nginx虚拟主机讲解完毕
配置基于端口的虚拟主机
server {
listen 8001;
server_name www.vipdailiang.com;
location / {
root html/www;
index www.html ; → 这里面内容改成 ism
}
}
server {
listen 8002;
server_name www.vipdailiang.com;
location / {
root html/bbs;
index bbs.html ; →这里面内容改成imp
}
}
netstat -tunpl | grep nginx ,能够看到nginx起了8001与8002两个端口
tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN 8398/nginx
tcp 0 0 0.0.0.0:8002 0.0.0.0:* LISTEN 8398/nginx
那么该怎么检测呢??
curl www.vipdailiang.com:8001
ism
curl www.vipdailiang.com:8002
imp
配置基于IP的虚拟主机
这个须要有多块网卡,nginx服务器须要多个ip
这个在生产环境中极为少见,这里就不作解释了
nginx 开启目录浏览
今天工做须要,要给客户提供一个patch的下载地址,因而想用nginx的目录浏览功能来作,须要让客户看到指定一个目录下的文件列表,而后让他本身来选择该下载那个文件;
咱们都知道在apache下能够配置访问web服务器的某个路径时,自动显示其目录下面的文件列表的,其实Nginx一点也不比apache弱,它固然也能够实现这个功能,并且还很是容易和简单;主要用到autoindex 这个参数来开启,其配置以下:
代码以下:
location / {
alias /software/tools/; #注意tools后面的/ ,加/ 与不加 这个有区别的
autoindex on; #//开启目录浏览功能;
autoindex_exact_size off; # //关闭详细文件大小统计,让文件大小显示MB,GB单位,默认为b;
autoindex_localtime on; #//开启以服务器本地时区显示文件修改日期!
}
注意:
ok。。至此nginx配置文件就讲解完成了