长春哪家做网站做的好,智能建站系统官网,兰州装修公司官网,推进文明网站建设全局配置
NGINX配置信息
nginx 官方帮助文档#xff1a;http://nginx.org/en/docs/Nginx的配置文件的组成部分#xff1a;
主配置文件#xff1a;/conf/nginx.conf(/nginx/conf/nginx.conf)
子配置文件: include conf.d/*.conf#事件驱动相关的配置 同步
event {
worker_…全局配置
NGINX配置信息
nginx 官方帮助文档http://nginx.org/en/docs/Nginx的配置文件的组成部分
主配置文件/conf/nginx.conf(/nginx/conf/nginx.conf)
子配置文件: include conf.d/*.conf #事件驱动相关的配置 同步
event {
worker_connections 1024; #一次允许1024个执行...
} #http/https 协议相关配置段
http {
server{
location{}
}...
}#默认配置文件不包括下面两个块
#mail 协议相关配置段
mail {...
}#stream 服务器相关配置段
stream {负载均衡...
}
修改启动进程数
lscpu |grep -i cpu #查看cup核数
[rootlocalhost ~]vim /apps/nginx/conf/nginx.conf
#开启 两核
#user nobody;
worker_processes 4; #根据CPU核数修改
worker_processes auto; #如果设置为auto 就是你真实的cpu数量关闭或修改版本
关闭版本
vim /apps/nginx/conf/nginx.conf
#修改配置文件 放在 http语句中
http {server_tokens off; }nginx -s reload修改版本
#在安装包中
[rootlocalhost core] vim /opt/nginx-1.18.0/src/core/nginx.h
#define NGINX_VERSION 9527
#define NGINX_VER http/ NGINX_VERSION[rootlocalhost core] vim /opt/nginx-1.18.0/src/http/ngx_http_header_filter_module.c
static u_char ngx_http_server_string[] Server: beijing CRLF;[rootlocalhost nginx-1.18.0]./configure --prefix/apps/nginx --usernginx --groupnginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[rootlocalhost nginx-1.18.0]make make install修改pid文件路径
mkdir /apps/nginx/run/ #创建目录
vim /apps/nginx/conf/nginx.conf #修改配置文件
pid /apps/nginx/run/nginx.pid; #找到 pid的位置修改
[rootlocalhost ~]#nginx -s reload CPU与work进程绑定
默认Nginx是不进行进程绑定的但是绑定了以后可以可以保证此进程不会运行在其他核心上这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转减少了CPU对进程的资源分配与回收以及内存管理等因此可以有效的提升nginx服务器的性能。
CPU序号
CPU MASK: 000000010号CPU000000101号CPU................100000007号CPUps axo pid,cmd,psr,ni|grep -v grep |grep nginx|sort -n #这个命令可以查看每个进程占用的是那个CPU
[rootlocalhost ~]vim /apps/nginx/conf/nginx.conf
worker_cpu_affinity 00000001 00000010 00000100 00001000;
[rootlocalhost ~]nginx -s reload 进程的优先级
nice的优先级范围是-20~19[rootlocalhost ~] ps axo pid,cmd,psr,ni|grep nginx|sort -n #查看优先级
[rootlocalhost ~]vim /apps/nginx/conf/nginx.conf
worker_priority -20; #将优先级调为-20
[rootlocalhost ~]nginx -s reload 调试work进程打开的文件个数
[rootlocalhost ~]vim /apps/nginx/conf/nginx.conf
worker_rlimit_nofile 65536; #一次允许65536访问
events {worker_connections 20000; #最大连接数
}
[rootlocalhost security]#nginx -s reload临时修改
[rootlocalhost ~]#ulimit -n 60000 #只修改当前窗口
永久修改
[rootlocalhost security]#vim /etc/security/limits.conf #将下面内容直接写在文件末尾
* soft core unlimited
* hard core unlimited
* soft nproc 1000000
* hard nproc 1000000
* soft nofile 1000000
* hard nofile 1000000
* soft memlock 32000
* hard memlock 32000
* soft msgqueue 8192000
* hard msgqueue 8192000永久修改后需要重启reboot才能生效临时的不用重启[rootlocalhost security]ulimit -a #可以查看
服务前后台运行
一般都是后台运行前台运行容器中会用到
[rootlocalhost ~]vim /apps/nginx/conf/nginx.conf
daemon off; #关闭后台运行
rootlocalhost security]nginx -s reloadecent部分
在Nginx的主配置文件中events部分用于配置Nginx服务器的事件模块相关参数控制Nginx服务器在处理连接请求时的行为。
常见的events配置参数
worker_connections指定每个工作进程可以同时处理的最大连接数。
multi_accept指定是否一次接受多个连接。默认情况下Nginx在每个循环中只接受一个连接但设置multi_accept为on后可以同时接受多个连接。
use指定Nginx使用的事件模块。常见的事件模块有epoll、kqueue和eventport等。
events {worker_connections 1024;multi_accept on;use epoll;
}
#指定了每个工作进程可以处理的最大连接数为1024启用了多个连接同时接受以及使用了epoll事件模块以下内容是在yum安装下NGINX的相关配置
HTTP设置
http协议配置说明
include mime.types; #导入支持的文件类型,是相对于/apps/nginx/conf的目录
sendfile on;#自定义优化参数
gzip on; #开启文件压缩
listen 80; #设置监听地址和端口
server_name localhost; #设置server name
charset koi8-r; #设置编码格式默认是俄语格式建议改为utf-8
error_page 500 502 503 504 /50x.html; #定义错误页面mime
在Nginx中“mime” 是一种配置指令用于设置 MIME 类型与文件扩展名的映射关系。 此项为支持的 文件格式如果不支持的格式 会自动帮你下载如果支持 就会显示在网页上
vim /etc/nginx/mime.types
#查看当前Nginx服务器配置的MIME类型列表server下的root指令
在Nginx配置中root指令用于设置服务器块的根目录即指明软件的根目录。 通常root指令位于Nginx配置文件中的服务器块中。
server {listen 80;server_name example.com;root /var/www/html;#指定了服务器块的根目录为/var/www/htmllocation / {...} ...
}server块构建虚拟主机
基于域名
[rootlocalhost ~] vim /etc/nginx/nginx.conf
#修改配置文件 要放在 http 模块里include /etc/nginx/mime.types;include /apps/nginx/conf.d/*.conf;[rootlocalhost ~] cd /etc/nginx/conf.d[rootlocalhost conf.d] vim test1.conf #编写子配置文件server {listen 80;server_name www.test1.com; #域名root /data/nginx/html/test1/; #网页目录位置
}
[rootlocalhost conf.d] vim test2.conf
server {listen 80;server_name www.test2.com;root /data/nginx/html/test2/;
}构建数据文件夹
[rootlocalhost ~] mkdir -p /data/nginx/html/test1
[rootlocalhost ~] mkdir -p /data/nginx/html/test2
构建数据文件
[rootlocalhost ~] echo test1 /data/nginx/html/test1/index.html
[rootlocalhost ~] echo test2 /data/nginx/html/test2/index.html[rootlocalhost conf.d] nginx -s reload去第二台机器修改/etc/hosts 文件
[rootlocalhost ~] vim /etc/hosts
192.168.65.110 www.test1.com
192.168.65.110 www.test2.com
[rootlocalhost ~] curl www.test1.com
test1
[rootlocalhost ~] curl www.test2.com
test2
基于端口
[rootlocalhost conf.d] vim test1.confserver {listen 80;server_name www.test1.com; #域名root /data/nginx/html/test1/; #网页目录位置
}
[rootlocalhost conf.d] vim test2.conf
server {listen 8080;server_name www.test2.com;root /data/nginx/html/test2/;
}
[rootlocalhost conf.d] nginx -s reload第二台机器
[rootlocalhost ~] curl 192.168.65.110
test1
[rootlocalhost ~] curl 192.168.65.110:8080
test2
基于ip地址
[rootlocalhost conf.d] vim test1.confserver {listen 129.168.65.110;server_name www.test1.com; #域名root /data/nginx/html/test1/; #网页目录位置
}
[rootlocalhost conf.d] vim test2.conf
server {listen 192.168.65.103;server_name www.test2.com;root /data/nginx/html/test2/;
}
[rootlocalhost conf.d]#nginx -s reload[rootlocalhost conf.d]#ifconfig ens33:0 192.168.65.103/24 #添加虚拟网卡第二台机器
[rootlocalhost ~]#curl 192.168.65.110
test1
[rootlocalhost ~]#curl 192.168.65.103
test2
alias别名
server {listen 80;server_name www.test.com;location /nwes {root /data/nginx/html/ag/;#相当于追加 将 文件夹news追加到/data/nginx/html/ag/news}location /study{alias /mnt/nginx/sports/;#相当于替换 你访问 study 就是访问/mnt/nginx/sports}
}location
http://nginx.org/en/docs/http/ngx_http_core_module.html#location #官方文档#匹配优先级从高到低
, ^~, ~/~*, 不带符号 #用于标准url前需要请求字串与uri精确匹配大小敏感,如果匹配成功就停止向下匹配并立即处理请求
^~ #用于标准url前表示包含正则表达式,并且匹配以指定的正则表达式开头,对URI的最左边部分做匹配检查不区分字符大小写
~ #用于标准url前表示包含正则表达式,并且区分大小写
~* #用于标准url前表示包含正则表达式,并且不区分大写
不带符号 #匹配起始于此url的所有的url\ #用于标准url前表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号正则表达式匹配location ~* /A.?\.jpg { #匹配 包含A后面一个或没有字符已.jpg结尾的图片root /opt/nginx/html/image;}这个例子是区分大小写的一般将大写或小写文件复制一次避免出现问题access模块
http://nginx.org/en/docs/http/ngx_http_access_module.html #官方文档[rootlocalhost nginx-1.18.0] ./configure --help |grep access
# 可以去源码包中 过滤 access模块 自带 不想要可以 without 去掉 --without-http_access_module disable ngx_http_access_module--http-log-pathPATH set http access log pathname--without-stream_access_module disable ngx_stream_access_module子配置文件谁优先级高谁设置的生效按开头字母前后优先生效
配置文件修改在server内
server {listen 80;server_name www.test.com;allow 192.168.65.0/24; #允许这个网段访问deny 192.168.65.101; #不允许这个地址访问location / {root /data/nginx/html/ag;}
}配置文件修改在location内
server {listen 80;server_name www.test.com;location / {root /data/nginx/html/ag;allow 192.168.91.0/24;deny 192.168.91.101;}
}验证模块
火狐浏览器有谷歌没有
yum -y install httpd tools.x86_64 #安装这个包这个模块在这个包内
htpasswd
-c 代表新建用户名和密码对应的文件
-b 将密码跟在用户名后[rootlocalhost conf.d] htpasswd -bc /data/.httpuser ag 123456 #第一次创建加c
[rootlocalhost conf.d] htpasswd -b /data/.httpuser ttg 123456 #非第一次创建cd /data/nginx/html
mkdir admin
cd admin
touch index.html
server {listen 80;server_name www.test.com;root /data/nginx/html;location /admin {auth_basic welcome to ag;auth_basic_user-file /data/.httpuser;}
}自定义错误页面
格式
error_page code ... [[response]] uri;响应码 访问链接
server {listen 80;server_name www.test.com;root /data/nginx/html;error_page 404 /index.html;#出了错 直接跳转到显示主站点}server {listen 80;server_name www.test.com;root /data/nginx/html;error_page 404 301 /index.html;}#把错误码 404 指定成302 并跳到主页面/index.html日志
自定义日志位置
yum安装的日志文件位置 /var/log/nginx
error_log file [level];
固定格式 文件路径 日志级别[rootlocalhost error] vim /etc/nginx/conf.d/test.conf
server {listen 80;server_name www.test.com;root /data/nginx/html;error_page 404 301 /index.html;access_log /data/nginx/logs/rz-access.log;
}#生成了新的存放日志文件[rootlocalhost conf.d] nginx -s reload
自定义日志格式
vim /etc/nginx/nginx.conf
log_format access_json {timestamp:$time_iso8601,host:$server_addr,clientip:$remote_addr,size:$body_bytes_sent,responsetime:$request_time,upstreamtime:$upstream_response_time,upstreamhost:$upstream_addr, http_host:$host,uri:$uri,xff:$http_x_forwarded_for,referer:$http_referer,tcp_xff:$proxy_protocol_addr,http_user_agent:$http_user_agent,status:$status};日志分离
[rootlocalhost ~] cd /var/log/nginx/
[rootlocalhost nginx]mv access.log access-old.log
[rootlocalhost nginx]touch access-new.log
[rootlocalhost nginx]nginx -s reopen 将两个网站的日志分离
[rootlocalhost ~] vim/etc/nginx/conf.d/a.conf
server{listen 80;server_name www.a.com;root /data/nginx/a/;error_log /data/logs/a_error.log;access_log /data/logs/a_access.log;
}
[rootlocalhost ~] vim/etc/nginx/conf.d/b.conf
server{listen 80;server_name www.b.com;root /data/nginx/b/;error_log /data/logs/b_error.log;access_log /data/logs/b_access.log;
}
[rootlocalhost ~] mkdir /data/logs -p
[rootlocalhost ~] nginx -t
[rootlocalhost ~] nginx -s reload
检测文件是否存在
location / {root /data/nginx/html/pc;try_files $uri $uri.html $uri/index.html /about/default.html;}#你如果 访问一个路径 /kgc 如果没有这个页面 先去找kgc kgc.html 然后再去找 kgc/index.html 最后再 找/about/default.html
长连接
keepalive_timeout timeout [header_timeout];
#设定保持连接超时时长0表示禁止长连接默认为75s通常配置在http字段作为站点全局配置
keepalive_requests number;
#在一次长连接上所允许请求的资源的最大数量默认为100次,建议适当调大,比如:500
可以加在全局或者 server 作为下载服务器配置
http://nginx.org/en/docs/http/ngx_http_autoindex_module.html #官方文档[rootlocalhost nginx]# cd conf.d/
[rootlocalhost conf.d]# vim test.conf
location /download {autoindex on; #开启下载服务器autoindex_exact_size on; #开启确切大小不建议开启autoindex_localtime on; #使用当地时间limit_rate 1024k; #所有人限速1024k默认单位是字节数 alias /opt/download;}[rootlocalhost conf.d]# cd /opt
[rootlocalhost opt]# mkdir download
[rootlocalhost opt]# nginx -s reload
[rootlocalhost opt]# mount /dev/sr0 /opt/download/
mount: /dev/sr0 写保护将以只读方式挂载
用户上传资料
上传需要借助开发小的程序 并且程序 5M 和 nginx 10M 都会限制。 两者取最小
client_max_body_size 1m;
#设置允许客户端上传单个文件的最大值默认值为1m,上传文件超过此值会出413错误
client_body_buffer_size size;
#用于接收每个客户端请求报文的body部分的缓冲区大小;默认16k;超出此大小时其将被暂存到磁盘上的由下面client_body_temp_path指令所定义的位置
client_body_temp_path path [level1 [level2 [level3]]];
#设定存储客户端请求报文的body部分的临时存储路径及子目录结构和数量目录名为16进制的数字使用hash之后的值从后往前截取1位、2位、2位作为目录名上传文件大于限制 错误代码413高级配置
网页的状态页
[rootlocalhost html]# vim /etc/nginx/conf.d/test.conf
location /status {stub_status;
}
[rootlocalhost html]# nginx -s reload
Active connections 当前处于活动状态的客户端连接数包括连接等待空闲连接数readingwritingwaiting accepts 统计总值Nginx自启动后已经接受的客户端请求的总数。 handled 统计总值Nginx自启动后已经处理完成的客户端请求总数通常等于accepts除非有因worker_connections限制等被拒绝的连接 requests 统计总值Nginx自启动后客户端发来的总的请求数。 Reading 当前状态正在读取客户端请求报文首部的连接的连接数,数值越大,说明排队现象严重,性能不足 Writing 当前状态正在向客户端发送响应报文过程中的连接数,数值越大,说明访问量很大 Waiting 当前状态正在等待客户端发出请求的空闲连接数开启 keep-alive的情况下,这个值等于active – (readingwriting)
nginx第三方模块
echo模块 https://github.com/openresty/echo-nginx-module编译安装下添加的方法
下载安装包
unzip echo-nginx-module-master.zip #解压
./configure --prefix/apps/nginx --usernginx --groupnginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module/opt/echo-nginx-module-master #到nginx源代码下编译在编译安装nginx的位置
make make install
[rootlocalhost html]# vim /etc/nginx/conf.d/test.conf
location /hello {echo hello world ;
}
curl 192.168.65.106/hello
会直接出来不用添加新网页变量
内置变量
$remote_addr; #存放了客户端的地址注意是客户端的公网IP
$proxy_add_x_forwarded_for # 代理中所有经过的服务器的ip
$args; #变量中存放了URL中的参数
如:http://www.kgc.org/main/index.do?id20190221partnersearch
#返回结果为: id20190221partnersearch 存放的就是这个
$document_root; #保存了针对当前资源的请求的系统根目录
例如:/apps/nginx/html。
$document_uri; #保存了当前请求中不包含参数的URI
如:http://www.kgc.org/main/index.do?id20190221partnersearch会被定义为/main/index.do
#返回结果为:/main/index.do
$host; #存放了请求的host名称
limit_rate 10240;
echo $limit_rate;
#如果nginx服务器使用limit_rate配置了显示网络速率则会显示如果没有设置 则显示0
$remote_port; #客户端请求Nginx服务器时随机打开的端口这是每个客户端自己的端口
$remote_user; #已经经过Auth Basic Module验证的用户名
$request_body_file; #做反向代理时发给后端服务器的本地资源的名称
$request_method; #请求资源的方式GET/PUT/DELETE等
$request_filename; #当前请求的资源文件的磁盘路径由root或alias指令与URI请求生成的文件绝对路径
如:/apps/nginx/html/main/index.html
$request_uri; #包含请求参数的原始URI不包含主机名相当于:$document_uri?$args,例如/main/index.do?id20190221partnersearch
$scheme; #请求的协议例如:httphttps,ftp等
$server_protocol; #保存了客户端请求资源使用的协议的版本
例如:HTTP/1.0HTTP/1.1HTTP/2.0等
$server_addr; #保存了服务器的IP地址
$server_name; #请求的服务器的主机名
$server_port; #请求的服务器的端口号
$http_user_agent; #客户端浏览器的详细信息
$http_cookie; #客户端的cookie信息
$cookie_name #name为任意请求报文首部字部cookie的key名
$http_name #name为任意请求报文首部字段,表示记录请求报文的首部字段ame的对应的首部字段名需要为小写如果有横线需要替换为下划线
自定义模块
[rootlocalhost data]# vim /etc/nginx/conf.d/test.conf
location /test {set $name test;echo $name;set $my_port $server_port;echo $my_port;}
[rootlocalhost data]# nginx -s reload
[rootlocalhost data]#curl 192.168.65.106/tset
test
80nginx压缩功能
太小的文件没必要压缩压缩可能会变大
官方文档 https://nginx.org/en/docs/http/ngx_http_gzip_module.htmlgzip on | off; #启用或禁用gzip压缩默认关闭
gzip_comp_level level;#压缩比由低到高从1到9默认为1
gzip_min_length 1k; #gzip压缩的最小文件小于设置值的文件将不会压缩
gzip_http_version 1.0 | 1.1; #启用压缩功能时协议的最小版本默认HTTP/1.1
gzip_buffers number size; #指定Nginx服务需要向服务器申请的缓存空间的个数和大小,平台不同,默认:32 4k或者16 8k;
gzip_types mime-type ...; #指明仅对哪些类型的资源执行压缩操作;默认为gzip_types text/html不用显示指定否则出错
gzip_vary on | off;#如果启用压缩是否在响应报文首部插入“Vary: Accept-Encoding”,一般建议打开
gzip_static on | off;#预压缩server {root /usr/share/nginx/html;gzip on;gzip_comp_level 9;gzip_min_length 1k;gzip_vary on;}https功能
官方文档https://nginx.org/en/docs/http/ngx_http_ssl_module.htmllisten 443 ssl; #为指定的虚拟主机配置启用ssl功能
ssl_certificate /path/to/file;
#指向包含当前虚拟主机和CA的两个证书信息的文件一般是crt文件
ssl_certificate_key /path/to/file;
#当前虚拟主机使用的私钥文件一般是key文件
ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];
#支持ssl协议版本早期为ssl现在是TLS默认为后三个自定义图标
#方法一服务器不记录访问日志
location /favicon.ico {log_not_found off;access_log off;
}
#方法二将图标保存到指定目录访问
#location ~ ^/favicon\.ico$ {
location /favicon.ico {root /data/nginx/html/pc/images;expires 365d; #设置文件过期时间
}