配置多个域名的虚拟主机
nginx可以自动识别用户请求的域名,根据不同的域名请求服务器传输不同的内容,只需要保证服务器上有一个可用的ip地址,配置好dns解析服务。
/etc/hosts是linux系统中本地dns解析的配置文件,同样可以达到域名访问效果
C:\Windows\System32\drivers\etc\hosts文件,windows本地的测试域名
写入如下内容 192.168.13.79 www.s15rihan.com 192.168.13.79 www.s15oumei.com
修改nginx.conf
[root@oldboy_python ~ 14:33:16]#egrep -v '#|^$' /opt/nginx1-12/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.s15oumei.com; location /{ root /opt/myserver/oumei; index index.html index.htm; } }
server { listen 80; server_name www.s15rihan.com; location /{ root /opt/myserver/rihan; index index.html index.htm; } }
}
上述代码配置了www.s15oumei.com、www.s15rihan.com域名的站点,虚拟主机的部分就是server{}里的内容
创建www.s15oumei.com 、www.s15rihan.com 的站点目录和文件
[root@localhost myserver]# tree /opt/myserver/
/opt/myserver/
├── oumei
│ └── index.html 写入自己的内容
└── rihan
└── index.html 写入自己的内容
上述作用创建了一个html/oumei、html/rihan站点目录,对应于虚拟主机配置文件里的root根目录的设置 /opt/myserver/oumei、/opt/myserver/rihan
然后生成一个首页文件index.html,内容是“欧美电影”、"日韩电影"
检查nginx语法重新加载nginx
[root@oldboy_python /opt/nginx1-12/html 14:37:28]#../sbin/nginx -t
nginx: the configuration file /opt/nginx1-12/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx1-12/conf/nginx.conf test is successful
#平滑重启nginx
[root@oldboy_python /opt/nginx1-12/html 14:39:18]#../sbin/nginx -s reload
检查nginx端口,进程,访问www.s15oumei.com、www.s15rihan.com虚拟站点
[root@oldboy_python /opt/nginx1-12/html 14:40:02]#netstat -tunlp|grep nginx [root@oldboy_python /opt/nginx1-12/html 14:40:29]#ps -ef|grep nginx #我这里是有dns解析,没有的话则需要/etc/hosts解析 #成功配置了pyyuc虚拟主机站点 [root@oldboy_python /opt/nginx1-12/html 14:41:37]#curl www.s15oumei.com <meta charset=utf8>欧美电影
[root@oldboy_python /opt/nginx1-12/html 14:41:37]#curl www.s15rihan.com <meta charset=utf8>日韩电影
nginx访问日志(access_log)
日志功能对每个用户访问网站的日志信息记录到指定的日志文件里,开发运维人员可以分析用户的浏览器行为,此功能由ngx_http_log_module模块负责,官网地址是:
http://nginx.org/en/docs/http/ngx_http_log_module.html
控制日志的参数
log_format 记录日志的格式,可定义多种格式 accsss_log 指定日志文件的路径以及格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
对应参数解析
$remote_addr 记录客户端ip $remote_user 远程用户,没有就是 “-” $time_local 对应[14/Aug/2018:18:46:52 +0800] $request 对应请求信息"GET /favicon.ico HTTP/1.1" $status 状态码 $body_bytes_sent 571字节 请求体的大小 $http_referer 对应“-” 由于是直接输入浏览器就是 - $http_user_agent 客户端身份信息 $http_x_forwarded_for 记录客户端的来源真实ip 97.64.34.118
日志效果如下
66.102.6.6 - - [14/Aug/2018:18:46:52 +0800] "GET /favicon.ico HTTP/1.1" 404 571 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36 Google Favicon" "97.64.34.118"
nginx.conf默认配置
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main;
日志格式配置定义
log_format是日志关键字参数,不能变 main是日志格式指定的标签,记录日志时通过main标签选择指定的格式。
nginx限制网站来源IP访问
如果哪天发现你的nginx很慢,或者检查access.log时候,有一个some body疯狂请求你的nginx server,那么可以禁止这个IP访问
限制ip或ip段访问 禁止访问/av/底下的资源 location /av { deny 122.71.240.254; #alias /opt/nginx1-12/html/av; allow 10.1.1.0/16; }
Nginx错误页面优化
在网站运行过程中,可能因为页面不存在等原因,导致网站无法正常响应请求,此时web服务会返回系统的错误码,但是默认的错误页面很不友好。
因此我们可以将404,403等页面的错误信息重定向到网站首页或者其他指定的页面,提升用户访问体验。
server { listen 80; server_name www.pythonav.cn; root html/pythonav; location /{ index index.html index.htm; } #在pythonav路径下的40x.html错误页面 error_page 400 403 404 405 /40x.html; }
40x.html
<img style='width:100%;height:100%;' src=https://pic1.zhimg.com/80/v2-77a9281a2bebc7a2ea5e02577af266a8_hd.png>
此时访问www.pythonav.cn/asdasd错误页面已经优化了
Nginx代理
正向代理
正向代理,也就是传说中的代理,他的工作原理就像一个跳板(VPN),简单的说:
我是一个用户,我访问不了某网站,但是我能访问一个代理服务器,这个代理服务器呢,他能访问那个我不能访问的网站,于是我先连上代理服务器,告诉他我需要那个无法访问网站的内容,代理服务器去取回来,然后返回给我。
反向代理
对于客户端而言,代理服务器就像是原始服务器。
nginx实现负载均衡的组件
ngx_http_proxy_module proxy 代理模块,用于把请求抛给服务器节点或者upstream服务器池
实现一个简单的反向代理
机器准备,两台服务器
master 192.168.11.63 主负载 slave 192.168.11.64 web1
主负载均衡节点的配置文件
worker_processes 1; error_log logs/error.log; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; keepalive_timeout 65; upstream slave_pools{ server 192.168.11.64:80 weight=1; } server { listen 80; server_name localhost; location / { proxy_pass http://slave_pools; root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
检查语法并启动nginx
[root@master 192.168.11.63 /opt/nginx1-12]$/opt/nginx1-12/sbin/nginx -t nginx: the configuration file /opt/nginx1-12/conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx1-12/conf/nginx.conf test is successful
#启动nginx [root@master 192.168.11.63 /opt/nginx1-12]$/opt/nginx1-12/sbin/nginx #检查端口 [root@master 192.168.11.63 /opt/nginx1-12]$netstat -tunlp|grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8921/nginx: master
此时访问master的服务器192.168.11.63:80地址,已经会将请求转发给192.168.11.64:80
除了页面效果的展示以外,还可以通过log(access.log)查看代理效果
master端日志
slave端日志
nginx语法之location详解
Location语法优先级排列
匹配符 匹配规则 优先级 = 精确匹配 1 ^~ 以某个字符串开头 2 ~ 区分大小写的正则匹配 3 ~* 不区分大小写的正则匹配 4 !~ 区分大小写不匹配的正则 5 !~* 不区分大小写不匹配的正则 6 / 通用匹配,任何请求都会匹配到 7
nginx.conf配置文件实例
server { listen 80; server_name pythonav.cn; #优先级1,精确匹配,根路径 location =/ { return 400; } #优先级2,以某个字符串开头,以av开头的,优先匹配这里,区分大小写 location ^~ /av { root /data/av/; } #优先级3,区分大小写的正则匹配,匹配/media*****路径 location ~ /media { alias /data/static/; } #优先级4 ,不区分大小写的正则匹配,所有的****.jpg|gif|png 都走这里 location ~* .*\.(jpg|gif|png|js|css)$ { root /data/av/; } #优先7,通用匹配 location / { return 403; } }
nginx语法之root和alias区别实战
nginx指定文件路径有root和alias两种方法 区别在方法和作用域: 方法: root 语法 root 路径; 默认值 root html; 配置块 http{} server {} location{} alias 语法: alias 路径 配置块 location{} root和alias区别在nginx如何解释location后面的url,这会使得两者分别以不同的方式讲请求映射到服务器文件上 root参数是root路径+location位置 root实例: location ^~ /av { root /data/av; 注意这里可有可无结尾的 / } 请求url是pythonav.cn/av/index.html时 web服务器会返回服务器上的/data/av/av/index.html root实例2: location ~* .*\.(jpg|gif|png|js|css)$ { root /data/av/; } 请求url是pythonav.cn/girl.gif时 web服务器会返回服务器上的/data/static/girl.gif alias实例: alias参数是使用alias路径替换location路径 alias是一个目录的别名 注意alias必须有 "/" 结束! alias只能位于location块中 请求url是pythonav.cn/av/index.html时 web服务器会返回服务器上的/data/static/index.html location ^~ /av { alias /data/static/; }