Bash
Nginx配置文件简单详解
#主配置文件结构:四部
main block:主配置段,即全局配置段,对http,mail都有效
event {
...
} 事件驱动相关的配置
http {
...
} http/https 协议相关配置段
mail {
...
} mail 协议相关配置段
? stream {
...
} stream 服务器相关配置段
#http协议相关的配置结构
http {
...
... 各server的公共配置
server { 每个server用于定义一个虚拟主机
...
}
server {
...
server_name 虚拟主机名
root 主目录
alias 路径别名
location [OPERATOR] URL { 指定URL的特性
...
if CONDITION {
...
}
}
}
}
user
Syntax: user user [group];
Default: user nobody nobody;
Context: main
指定worker进程的运行身份,如组不指定,默认和用户名同名
pid /PATH/TO/PID_FILE
指定存储nginx主进程PID的文件路径
include file | mask
指明包含进来的其它配置文件片断
load_module file
模块加载配置文件:/usr/share/nginx/modules/*.conf
指明要装载的动态模块路径: /usr/lib64/nginx/modules
worker_connections number
每个worker进程所能够打开的最大并发连接数数量,如10240
总最大并发数:worker_processes * worker_connections
use method
指明并发连接请求的处理方法 ,默认自动选择最优方法use epoll;
accept_mutex on | off 互斥(建议不启用)
处理新的连接请求的方法;on指由各个worker轮流处理新请求,Off指每个新请求的到达都会通知(唤醒)所有的worker进程,但只有一个进程可获得连接,造成“惊群”,影响性能
daemon on|off
是否以守护进程方式运行nignx,默认是守护进程方式
master_process on|off
是否以master/worker模型运行nginx;默认为onoff 将不启动worker
error_log file [level]
错误日志文件及其级别;出于调试需要,可设定为debug;但debug仅在编译时使用了“--with-debug”选项时才有效
方式:file /path/logfile;
stderr:发送到标准错误
syslog:server-address[,parameter=values]:发送到syslog
memory:size 内存
level:debug|info|notice|warn|error|crit|alter|emerg
模块介绍
tcp_nodelay on | off;
在keepalived模式下的连接是否启用TCP_NODELAY选项当为off时,延迟发送,合并多个请求后再发送默认On时,不延迟发送
可用于:http, server, location
sendfile on | off;
是否启用sendfile功能,在内核中封装报文直接发送 默认Off
server_tokens on | off | build | string
是否在响应报文的Server首部显示nginx版本
Nginx优化:
修改源码隐藏版本号
# wget http://nginx.org/download/nginx-1.14.1.tar.gz
#tar zxvf nginx-1.14.1.tar.gz -C /usr/local/src/
#cd /usr/local/src/nginx-1.14.1/
隐藏Nginx版本号,需要一共修改3个源代码文件
1、修改nginx软件版本号
# vim src/core/nginx.h
改:
13 #define NGINX_VERSION "1.14.1"
14 #define NGINX_VER "nginx/" NGINX_VERSION
为:
#define NGINX_VERSION "8.8.2" #此行修改的是你想要的版本号。
#define NGINX_VER "XWS/" NGINX_VERSION #此行修改的是你想修改的软件名称。
2、修改HTTP头信息中的connection字段,防止回显具体版本号
[root@xuegod63 nginx-1.14.1]# vim src/http/ngx_http_header_filter_module.c
改:49 static char ngx_http_server_string[] = "Server: nginx" CRLF;
为:49 static char ngx_http_server_string[] = "Server: XWS" CRLF;
3、修改ngx_http_special_response.c文件定义了Nginx报 404错误时,不回显版本号。
注:nginx-1.14.1 版本,不再需要修改。 nginx1.10以下还需要修改以下内容:
[root@xuegod63 nginx-1.14.1]# vim src/http/ngx_http_special_response.c
35 static u_char ngx_http_error_tail[] =
改:36 "<hr><center>nginx</center>" CRLF
为:36 "<hr><center>XWS</center>" CRLF
模块参数隐藏版本
server_tokens on | off | build | string
默认为启用,如果不启用改成off
添加到http段为全局生效
添加到server段只为此server生效
性能优化相关的配置:
worker_processes number | auto
worker进程的数量;通常应该为当前主机的cpu的物理核心数,比如我有四核的一台服务器我只需要给3个进程的数量,留一个给系统调度使用
worker_cpu_affinity cpumask ... worker_cpu_affinity auto [cpumask] 提高缓存命中率
cpu亲和力
CPU MASK: 00000001:0号CPU
00000010:1号CPU
10000000:8号CPU
如果是四核绑定三个CPU就可以了,留一个核心给系统使用
绑定三核
worker_cpu_affinity 0001 0010 0100;
绑定四核CPU
worker_cpu_affinity 0001 0010 0100 1000;
绑定双核
worker_cpu_affinity 0101 1010;
worker_priority number
指定worker进程的nice值,设定worker进程优先级:[-20,20]
worker_rlimit_nofile number
worker进程所能够打开的文件数量上限,如65535
Nginx最多可以打开文件数
worker_cpu_affinity 0001 0010 0100 1000; #在这一行下面,插入以下内容
worker_rlimit_nofile 102400;
这个指令是指当一个Nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,即:(ulimit -n)/ worker_processes ,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。修改ulimit -n的值,可以参考linux系统调优的内容
nginx
server段:
location /img {
root /app/web01;
}
这里如果访问
www.a.com/img/123.jpg
这里的location的真实引用路径是 /app/web1/img/123.jpg
而不是/aap/web1/123.jpg
相当于是/app/web1/下的/img/目录里面的文件
精确匹配
server段
location = /img {
/app/web1;
}
精确输入img内容,才能匹配的到,这里的详细的目录是/app/web1/
路径别名
server段
location /img {
alias /app/web1;
}
访问www.a.com/img/123.jpg
这里的真实路径是/app/web1/123.jpg
路径别名
指定默认页面
server段
location /img {
alias /app/web1/;
index 123.jpg;
}
这里的意思是当你访问www.a.com/img/的时候后面不加上指定的文件名时,默认主页文件就是123.jpg
真实路径是/app/web1/123.jpg
指定错误页面引用,如404
server {
listen 80;
server_name www.a.com;
root /data/www.a.com;
error_page 404 /404.html;
location /404.html {
root /data/error_html;
}
location /img {
alias /app/web1/;
index 123.jpg;
}
}
指定错误页面指定路径指定地址,如上面的参数!!
error_page 404 /404.html
文件存放在/data/error_html/目录下,文件名叫404.html
location做引用路径
404条转302
server {
listen 80;
server_name www.a.com;
root /data/www.a.com;
error_page 404 =302 /302.html;
location /302.html {
root /data/error_html/;
}
location /img {
alias /app/web1/;
index 123.jpg;
}
}
error_page 404 =302
这是做跳转 404等于302
location /302.html {
root /data/error_html/;
}
location做引用系统路径,302.html真实存放路径在/data/error_html/302.html
404跳转302这样做的目的是防止浏览器劫持你的404,变成浏览器的广告
uri跳转
默认文件跳转
server段
server {
listen 80;
server_name www.a.com;
root /data/www.a.com;
location /img/ {
try_files $uri /123.jpg;
}
}
当访问www.a.com/img.111.jpg不存在的时候,这个会让他用一个默认的页面做访问。
默认页面是123.jpg
$uri 这里的后面跟的路径是你网站下的路径,比如你的网站路径是/data/www.a.com/
这里的路径就是你网站的目录里面的123.jpg这个文件
链接超时时间,默认超时时间75秒
http,server,location段可用
keepalive_timeout 75s;
限制客户端下载速度,访问速度
limit_rate 大小;
字节为单位
limit_rate 1024000;
设置下载速度1MB
限制指定的ip可以用get,其他ip全部都拒绝
server {
listen 80;
server_name www.a.com;
root /data/www.a.com;
keepalive_timeout 30s;
limit_rate 1024000;
location / {
limit_except GET {
allow 172.17.243.242;
deny all;
}
}
location /img/ {
try_files $uri /123.jpg;
}
}
限制写法:
只能location段添加
location / {
limit_except GET {
allow 192.168.1.0/24
deny all;
}
}
当你访问/根的时候除了get喝head之外其他方法允许192.168.1.0/24网段主机使用
测试方法:
curl -XPUT -T /etc/fstab http://www.a.com/
这是允许的结果
返回结果:
<html>
<head><title>405 Not Allowed</title></head>
<body>
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx</center>
</body>
</html>
不允许的结果:
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>
访问拒绝:指定192.168.1.2主机禁止访问
location / {
deny 192.168.1.2’
}
指定ip可以访问,其他网段拒绝
location / {
allow 192.168.1.3;
deny 192.168.1.0/24;
}
详细配置文件写法:
server {
listen 80;
server_name www.a.com;
root /data/www.a.com;
keepalive_timeout 30s;
limit_rate 1024000;
# location / {
# limit_except GET {
# allow 172.16.7.131;
# deny all;
# }
# }
location / {
allow 172.16.7.131;
deny all;
}
location /img/ {
try_files $uri /123.jpg;
}
}
配置文件自伤而下检查,一旦匹配,将生效,条件严格的置前
配置指定目录需要账号验证登录访问:
ocation /admin/ {
auth_basic "Admin Ares";
auth_basic_user_file /etc/nginx/.ngxpasswd;
}
这里的/admin/是网站的目录下的/admin/目录下,我的网站目录是/data/www.a.com/
所以这里是绝对路径是/data/www.a.com/admin/
生成密码需要用到httpd-tools这个软件包可以用yum安装一下
命令如下:
yum install httpd-tools -y
生成命令
第一次生成123账号和密码:
htpasswd -cm /etc/nginx/.ngxpasswd 123
输入密码
再输入密码
完成
第二次生成456账号和密码
htpasswd -m /etc/nginx/.ngxpasswd 456
输入密码
再次输入密码
完成
解释:/etc/nginx/.ngxpasswd是存放账号和密码的文件,123为用户名,第二次创建的时候不要加c否则会
清空原来的内容
可以单独给这个目录做一个访问控制只能哪个ip可以访问,
location /admin/ {
auth_basic “Admin Area”;
auth_basic_user_file /etc/nginx/.ngxpasswd
allow 172.16.7.131;
deny all;
}
监控访问状态
为了建议加上访问控制:
location /status {
stub_status;
allow 192.168.1.2;
deny 192.168.1.0/24;
}
访问地址监控访问状态:
http://www.a.com/status
日志:
$remote)adds
客户端ip地址
$remote_user
客户端用户名
[$time_local]
客户端访问时间
$request
客户端访问头部信息,就是用的是什么浏览器
$status
状态吗,请求返回状态码
$bytes_sent
数据大小,返回的数据多大
$http_referer
记录是从哪个地方找到我的网站,是直接输入网址,还是百度跳转而来
$http_user_agent
用户浏览器名称
$gzip_ratio
压缩比例
这样的目的是为了,让一部分的日志记录先写进内存里,这样可以减少服务器压力.
这样你查看日志的时候会有所延迟,
只能http段:
log_format compression '$remote_addr-$remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
server段:
access_log /datal/logs/nginx-access.log compression buffer=32k;
文件压缩:
gzip on|off
server {
listen 80;
server_name www.a.com;
root /data/www.a.com/;
gzip on;
gzip_http_version 1.0;
gzip_comp_level 1;
gzip_min_length 4k;
gzip_types text/plain;
access_log /data/logs/nginx-access.log compression buffer=32k;
error_page 404 /404.html;
location /404.html {
root /data/error_html/;
}
}
http版本压缩,这个意思就是http1.0以上的版本版本压缩
zip_http_version 1.0;
压缩率:1-9,数越大占用cpu越多
gzip_comp_level 1;
压缩大小启始值大小,如果启始值太小,反而压缩以后越大
gzip_min_length 4k;
压缩文件类型plain为txt类型
gzip_types text/plain;
实现ssl加密
实现私钥加密
先来安装软件
yum install mod_ssl -y
yum install openssl open-devle pcre-devel
进入目录修改文件
cd /etc/pki/tls/certs/
修改文件.这样做是为了下次启动nginx的不需要输入秘钥对
vim Makefile
编辑以下文件57行左右.把-aes128去掉即可
未修改的
%.key:
umask 77 ; \
/usr/bin/openssl genrsa -aes128 $(KEYLEN) > $@
s
修改过的
%.key:
umask 77 ; \
/usr/bin/openssl genrsa $(KEYLEN) > $@
生成加密秘钥,比如你的网站是www.a.com这里就写上www.a.com.crt就可以了
make www.a.com.crt
Country Name (2 letter code) [XX]: 这里是让你输入你在哪个国家
State or Province Name (full name) []:这里让你输入你所在的省份
Locality Name (eg, city) [Default City]:这里输入你所在的市区是哪里
Organization Name (eg, company) [Default Company Ltd]:这里输入你域名所绑定的公司
Organizational Unit Name (eg, section) []:所在的部门
Common Name (eg, your name or your server's hostname) []:你域名地址比如www.a.com
Email Address []:联系用的邮箱
这样在当前目录下就会生成两个文件,把这两个文件拷贝到你的虚拟机主机的目录下即可
移动走生成的秘钥文件
mv www.a.com.key www.a.com.crt /etc/nginx/conf.d/
___________________________________________
sl on|off
server {
listen 443 ssl;
server_name www.a.com;
root /data/www.a.com/;
ssl_certificate /etc/nginx//conf.d/ssl/www.a.com.crt;
ssl_certificate_key /etc/nginx/conf.d/ssl/www.a.com.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
gzip on;
gzip_comp_level 1;
gzip_min_length 4k;
gzip_types text/plain;
access_log /data/logs/nginx-access.log compression buffer=32k;
error_page 404 /404.html;
location /404.html {
root /data/error_html/;
}
}
如果没有修改Makefile
去掉加密文件的key去掉私钥去掉口令
openssl rsa -in a.key -out aa.key
这样在启动nginx的时候就不需要输入密码了
rewrite跳转
当你访问www.a.com/bbs/任意内容的时候跳转到www.a.com/forum/下的任意内容
server {
listen 80;
server_name www.a.com;
root /data/www.a.com/;
gzip on;
gzip_comp_level 1;
gzip_min_length 4k;
gzip_types text/plain;
access_log /data/logs/nginx-access.log compression buffer=32k;
location /bbs/ {
rewrite ^/bbs(.*)$ /forum$1 last;
}
error_page 404 /404.html;
location /404.html {
root /data/error_html/;
}
}