最近看了好多关于nginx的内容,和文章,也试着用了些。想着总结一下,方便以后查阅。毕竟像这种琐碎的知识点,不可能说在不用的情况下还能一直牢记。
本文即是这样而来。其中的知识点都是常用的——不管是初学者,还是有些许经验的开发者。但是笔者实在不认为它适合“零基础小白”,因为笔者不觉得能比文档之类的更能把基础知识点讲明白。
nginx环境
“nginx是一款轻量级HTTP服务器,采用事件驱动的异步非阻塞处理方式的框架。这让其具有极好的IO性能,时常用于服务端的反向代理和负载均衡。”
nginx优点 :支持海量高并发(采用IO多路复用epoll)、内存消耗少、配置简单、免费可商业化使用。
必要程序的安装 —— Linux环境:
yum -y install gcc gcc-c++ autoconf pcre-devel make automake
yum -y install wget httpd-tools vim
新建yum源:
vim /etc/yum.repos.d/nginx.repo
进入后:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch #我们使用的是centos7.x版本
gpgcheck=0
enabled=1
保存并退出
然后我们来执行以下命令:(查看yum是否已存在)
yum list | grep nginx
nginx配置文件
查看nginx安装目录:
rpm -ql nginx
- rpm:Linux的rpm包管理工具
- q选项:询问模式
- l选项:返回列表展示
我将nginx.conf和conf.d文件夹下的default.conf两个配置文件几乎做了详细备注,列出如下:
//nginx.conf
#运行用户,默认即是nginx,可以不进行设置
user nginx;
#Nginx进程,一般设置为和CPU核数一样
worker_processes 1;
#错误日志存放目录
error_log /var/log/nginx/error.log warn;
#进程pid存放位置
pid /var/run/nginx.pid;
events {
worker_connections 1024; # 单个后台进程的最大并发数
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main; #nginx访问日志存放位置
sendfile on; #开启高效传输模式
#tcp_nopush on; #减少网络报文段的数量
keepalive_timeout 65; #保持连接的时间,也叫超时时间
#gzip on; #开启gzip压缩
include /etc/nginx/conf.d/*.conf; #包含的子配置项位置和文件
//default.conf
server {
listen 8081; #配置监听端口
server_name localhost; //配置域名
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html; #服务默认启动目录
index index.html index.htm; #默认访问文件
}
#error_page 404 /404.html; # 配置404页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #错误状态码的显示页面,配置后需要重启
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
nginx服务启动、停止、重启
启动: 两种方式
nginx
systemctl start nginx.service
停止:
- 从容停止:
nginx -s quit
- 立即停止:
nginx -s stop
- 暴力停止:
killall nginx
systemctl stop nginx.service
重启:
systemctl restart nginx.service
查看服务器状态:
ps aux | grep nginx
相关实践:
- 更改配置文件后重载:
nginx -s reload
(不用重启服务器) - 查看端口号占用情况:
netstat -tlnp
自定义错误页 & 访问设置
在/etc/nginx/conf.d/default.conf
下可以看到一句话:
error_page 500 502 503 504 /50x.html;
其表示在http错误码为500、502、503、504时,转向网站根目录下的/50.html
进行处理。
同样的还有:
error_page 404 http://cjxnsb.cn/mxcf
有时我们的服务器只允许特定主机访问 —— 比如内部OA系统,这时我们就需要控制一些IP访问。
我们可以直接在location里配置:(隶属于文件default.conf)
location /{
deny IP地址; #禁止访问IP
allow IP地址; #允许访问IP
}
若用IP地址组,需加“/”:deny 182.116.193.67/200;
nginx访问权限处理
location中设置的权限是“从上到下”执行的。比如在deny中设置了all,则就不再执行allow选项了。
在工作中,访问权限的控制需求非常复杂,例如,对于网站下的img(图片目录)是允许所有用户访问,但对于网站下的admin目录则只允许公司内部固定IP访问。这时候仅靠deny和allow这两个指令,是无法实现的。我们需要location块来完成相关的需求匹配。
精确匹配符“=”的使用:
location =/img{
allow all;
}
location =/admin{
deny all;
}
正则表达式的使用——“~”
比如:以“php”结尾的文件都禁止外部用户访问:
location ~\.php${
deny:all;
}
nginx配置“虚拟主机”
常用的有“基于端口号的、基于IP的、基于域名的”,这里我们看下基于端口号设置——其原理就是:nginx监听多个端口,根据不同端口号,区分不同网站。
我们当然可以在主文件 etc/nginx/nginx.conf
或子配置文件etc/nginx/conf.d/default.conf
里设置,或者在conf.d文件夹下再建一个文件。而我们要做的,是修改(添加)server选项:如
server{
listen 8081;
server_name localhost;
root /usr/share/nginx/html/html8081;
index index.html;
}
我们在/usr/share/nginx/html/html8081/
目录下编写index.html 即可访问查看结果。
基于IP的配置和基于端口号的配置基本无二,只是把server_name值设置成IP地址(如127.0.0.1的形式)即可。
nginx使用域名设置虚拟主机
在实际中,我们实际上用的是“域名”的方式设置。
一般来说,你可能是在阿里云上买的域名(推荐)。点击“域名”后,你会看到你买过的域名,然后点击“解析”,即可得到(二级)域名。比如:
笔者用nginx.mxcfq.top
这个域名映射到默认的nginx首页位置,用nginx2.mxcfq.top
映射到上面设置的8001端口位置。
我们去vim etc/nginx/conf.d/default.conf
文件:修改如下
listen 80;
server_name nginx.mxcfq.top;
再到vim etc/nginx/conf.d/8001.conf
:修改如下
server{
listen 80;
server_name nginx2.mxcfq.top;
root /usr/share/nginx/html/html8081;
index index.html;
}
重启后,分别访问上面两个server_name,即可查看结果。
nginx反向代理
反向代理跟代理正好相反(需要说明的是,现在基本所有的大型网站的页面都是用了反向代理),客户端发送的请求,想要访问server服务器上的内容。发送的内容被发送到代理服务器上,这个代理服务器再把请求发送到自己设置好的内部服务器上,而用户真实想获得的内容就在这些设置好的服务器上。
比如:现在我们要访问http://nginx2.mxcfq.top
然后反向代理到jspang.com
这个网站。我们直接到etc/nginx/conf.d/8001.conf
配置文件里进行修改。
server{
listen 80;
server_name nginx2.mxcfq.top;
location / {
proxy_pass http://jspang.com;
}
}
(一般我们反向代理的都是一个IP,但是我这里代理了一个域名也是可以的。其实这时候我们反向代理就算成功了,我们可以在浏览器中打开http://nginx2.jspang.com
来测试一下)
nginx开启压缩gzip
打开nginx.conf配置文件:
http{
//...
gzip on;
gzip_proxied any; #nginx做前端代理时,无条件启用压缩
gzip_min_length 要压缩页面的最小大小;
gzip_comp_level 压缩级别; #设置压缩级别,最大为9,最小为1,此值越大,压缩时间越短
gzip_types text/plain text/css application/x-javascript application/javascript application/xml;
gzip_static on;
gzip_http_version 1.0;
add_header Vary Accept-Encoding gzip; #代替指令gzip_vary on;,用于在使用gzip功能时发送带有Vary:Accept-Encoding头域的响应头部——这对于本身不支持gzip压缩的客户端浏览器是有用的
//...
server{
//...
}
}
针对IE6上gzip开启问题,而且现在IE6几乎“无人问津”的情况,笔者认为,我们可以放弃IE6:
gzip_disable "MSIE [1-6]\.";
nginx负载均衡
当一个域名指向多台web服务器,添加一台nginx负载均衡服务器,将客户端请求“平均”发送给每台web服务器,避免单台服务器因负载过高而其余服务器空闲而出现的损失
新建配置文件:
vi /etc/nginx/conf.d/test.conf
upstream test{ #有多个文件,此处配置多个(test——自定义名)
ip_hash;
server 192.168.0.1:80 weight=100;
server 192.168.0.2:80 weight=50;
}
server{
listen 80;
server_name cjxnsb.cn;
location /{
proxy_pass http://test;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
哦对了,修改完配置,不要忘了重启服务器!