Nginx入门篇(二)之Nginx部署与配置文件解析

时间:2023-02-14 20:29:35

一、Nginx编译安装

()查看系统环境
[root@localhost tools]# cat /etc/redhat-release
CentOS Linux release 7.4. (Core)
[root@localhost tools]# uname -r
3.10.-.el7.x86_64
[root@localhost tools]# uname -m
x86_64 ()安装pcre、openssl依赖
pcre为正则依赖的库包文件,openssl在使用https时会用到
[root@localhost tools]# yum install -y pcre pcre-devel openssl openssl-devel
[root@localhost tools]# rpm -qa pcre pcre-devel
pcre-devel-8.32-.el7.x86_64
pcre-8.32-.el7.x86_64
[root@localhost tools]# rpm -qa openssl openssl-devel
openssl-devel-1.0.2k-.el7.x86_64
openssl-1.0.2k-.el7.x86_64 ()下载nginx,并解压
[root@localhost tools]# wget http://nginx.org/download/nginx-1.15.1.tar.gz
[root@localhost tools]# tar -zxf nginx-1.15..tar.gz ()编译安装
[root@localhost tools]# useradd -s /sbin/nologin -M nginx
[root@localhost tools]# cd nginx-1.15.
[root@localhost nginx-1.15.]# ./configure --help
[root@localhost nginx-1.15.]# ./configure \
--user=nginx \ #配置进程用户权限
--gourp=nginx \ #配置进程用户组权限
--prefix=/usr/local/nginx1.15.1 \ #指定安装路径
--with-http_stub_status_module \ #使用激活状态信息模块
--with-http_ssl_module #使用ssl功能模块
[root@localhost nginx-1.15.]# make && make install
[root@localhost nginx-1.15.]# ln -sv /usr/local/nginx1.15.1 /usr/local/nginx
‘/usr/local/nginx’ -> ‘/usr/local/nginx1.15.1’ ()启动
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t #检查配置文件语法
nginx: the configuration file /usr/local/nginx1.15.1/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx1.15.1/conf/nginx.conf test is successful
[root@localhost ~]# /usr/local/nginx/sbin/nginx #启动nginx
[root@localhost ~]# netstat -tunlp |grep 80  #检查监听端口
tcp 0.0.0.0: 0.0.0.0:* LISTEN /nginx: master

浏览器访问:http://192.168.56.11,会出现nginx的欢迎页

Nginx入门篇(二)之Nginx部署与配置文件解析

二、Nginx目录结构说明

[root@localhost ~]# tree /usr/local/nginx
/usr/local/nginx
├── client_body_temp
├── conf                #Nginx主配置文件目录
│ ├── fastcgi.conf         #fastcgi相关参数的配置文件
│ ├── fastcgi.conf.default    #fastcig的原始备份文件
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types          #媒体类型文件
│ ├── mime.types.default
│ ├── nginx.conf          #Nginx默认主配置文件
│ ├── nginx.conf.default
│ ├── scgi_params          #scgi相关参数文件,一般不用
│ ├── scgi_params.default
│ ├── uwsgi_params          #uwsgi相关参数文件,一般不用
│ ├── uwsgi_params.default
│ └── win-utf
├── fastcgi_temp      #fastcgi临时数据目录
├── html            #Nginx编译安装默认站点目录
│ ├── 50x.html      #错误页面优雅显示,当出现502时会调用该页面
│ └── index.html     #默认的首页文件
├── logs          #默认的日志目录
│ ├── access.log    #访问日志
│ ├── error.log     #错误日志
│ └── nginx.pid    #nginx的pid文件,Nginx进程启动后,会把所有进程的ID号写到该文件
├── proxy_temp      #临时目录
├── sbin          #Nginx的命令目录
│ └── nginx
├── scgi_temp       #临时目录
└── uwsgi_temp      #临时目录

三、nginx.conf配置文件解析

user nginx nginx;  #定义Nginx运行的用户和用户组

worker_processes auto;    #指定nginx的work进程数,建议设置为等于CPU总核心数。

worker_cpu_affinity    ;    #默认情况下,Nginx的多个进程可能都泡在某一个CPU或cpu某一核上,导致资源不均衡。可通过该参数令资源更均衡,此例子为4核

error_log /var/log/nginx/error.log info;    #全局错误日志定义类型,可以是相对路径,也可以是绝对路径。除了可以在此处定义,还可以在http、server、location中定义[ debug | info | notice | warn | error | crit ]

pid /var/run/nginx.pid;        #进程PID文件所在的路径,可以是相对路径,也可以是绝对路径。

worker_rlimit_nofile ;    #定义nginx最多打开文件数限制。如果没设置的话,这个值为操作系统(ulimit -n)的限制保持一致。把这个值设高,nginx就不会有"too many open files"的问题

events
{ use epoll; #参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux .6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。 worker_connections ; #定义每个work_process同时开启的最大连接数,即允许最多只能有这么多连接。 accept_mutex on;  #当某一个时刻只有一个网络连接请求服务器时,服务器上有多个睡眠的进程会被同时叫醒。这样会损耗一定的服务器性能。Nginx中的accept_mutex设置为on,将会对多个Ningx进程(worker processer)接收连接时进行序列化,防止多个进程争抢资源。默认就是on multi_accept on;  #nginx worker processer可以做到同时接收多个新到达的网络连接,前提是把该参数设置为on。默认为off,即每个worker processer一次只能接收新到达的网络连接。
} #设定http服务器
http
{
include mime.types; #Nginx支持的媒体类型库文件
default_type application/octet-stream; #默认媒体类型
#charset utf-; #默认编码
server_names_hash_bucket_size ; #服务器域名的hash表大小
client_header_buffer_size 32k; #客户端请求头部缓冲大小,如果请求头部大小大于指定缓冲区,则使用large_client_header_buffers指令分配更大的缓冲区
large_client_header_buffers 64k; #用于读取大型客户端请求头部的缓冲区的最大数量和大小,这些缓冲区仅在缺省缓冲区不足时按需分配。当处理请求或连接转换到保持活动状态时,释放缓冲区。
client_max_body_size 8m; #上传文件大小限制。设置nginx能处理最大请求主体大小,如果请求大于指定大小,则nginx会返回413错误。如果服务器处理大文件上传时,该指令尤为重要。
sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。
tcp_nopush on; #防止网络阻塞
tcp_nodelay on; #防止网络阻塞
keepalive_timeout ; #长连接超时时间,单位是秒
server_tokens off; #隐藏Nginx软件版本号信息,避免站点根据版本查找漏洞攻击 #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
fastcgi_connect_timeout ;  #Nginx和后端FastCGI服务器连接超时时间,默认60s,通常不要超过75秒,因为建立的连接越多,消耗资源越多。
fastcgi_send_timeout ;   #Nginx允许FastCGI服务器返回数据的超时时间,即在规定时间内后端服务器必须传完数据,否则Nginx断开连接,默认60s。
fastcgi_read_timeout ; #Nginx从FastCGI服务器端读取响应信息的超时时间,表示连接建立后,Nginx等待后端服务器的响应时间,是Nginx已经进入后端的排队之中等候处理的时间。
fastcgi_buffer_size 64k;    #指定读取fastcgi应答第一部分需要的缓冲区大小,默认缓冲区大小为fastcgi_buffers指令中每块的大小
fastcgi_buffers 64k;     #指定本地需要用多少或多大的缓冲区来缓冲fastcgi的应答请求,如果一个php脚本所产生的页面大小为256KB,那么回分配4个64KB的缓冲区来缓存,如果页面大小大于256KB,那么大于256KB的部分会缓存到fastcgi_temp_path指定的路径中。一般这个值应该为站点中php脚本所产生的页面大小的中间值,如果站点大部分脚本所产生的页面大小为256KB,那么这个值可以设置为"8 32K"、"4 64K"等。
fastcgi_busy_buffers_size 128k;  #指定系统繁忙时期可以使用的缓冲区大小,建议设置为fastcgi_buffers的2倍。
fastcgi_temp_file_write_size 128k;  #在写入fastcgi_temp_path时将用到多大的数据块,默认值为fastcgi_buffers的2倍,该数值设置过小,当负载过高时可能会出现502的错误。范围128-256KB
fastcgi_cache_path /data/ngx_fcgi_cache levels=: keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g; #配置缓存目录 #gzip模块设置
gzip on; #开启gzip压缩功能
gzip_min_length 1k; #最小压缩文件大小,页面字节数从header头部的Content-Length中获取,默认是0,不管页面多大,建议设置成大于1k,如果小于1k可能会越压越大
gzip_buffers 16k; #压缩缓冲区,表示申请4个单位为16k的内存作为压缩结果流缓存,默认值是申请和原始数据大小相同的内存空间来存储gzip压缩结果
gzip_http_version 1.0; #压缩版本(默认1.,前端如果是squid2.5请使用1.)
gzip_comp_level ; #压缩等级,1压缩比最小,处理速度最快;9压缩比最大,但是处理慢,也比较消耗CPU资源
gzip_types text/plain application/x-javascript text/css application/xml; 默认压缩类型,text/html类型默认压缩
gzip_vary on;   #varyhead支持,该选项可以让前端缓存服务器经过GZIP压缩页面,例如用Squid缓存经过nginx压缩的数据
limit_conn_zone $binary_remote_addr zone=addr:10m;  #开启限制并发数
#limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;  #开启限制ip的请求数 upstream bbs.abc.org {
#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
server 192.168.56.11: weight=;
server 192.168.56.12: weight=;
server 192.168.56.13: weight=;
} #虚拟主机的配置
server
{
#监听端口
listen ;
#域名可以有多个,用空格隔开
server_name www.bbs.org;
index index.html index.htm index.php;
root /data/www/bbs;
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_cache ngx_fcgi_cache;
fastcgi_cache_valid 1h; #用于指定应答代码的缓存时间,实例中的值将200和302应答缓存1个小时
fastcgi_cache_valid 1d; #将301应答代码缓存1天
fastcgi_cache_valid any 1m; #将其他应答代码缓存1分钟
fastcgi_cache_min_uses ; #设置请求几次之后响应将被缓存,1表示1次即被缓存
fastcgi_cache_use_stale error timeout invalid_header http_500; #定义在哪些情况下使用过期缓存
fastcgi_cache_key http://$host$request_uri; #定义fastcgi_cache的key,Nginx会取这个key的md5作为缓存文件
}
#图片缓存时间设置
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 10d; #缓存10天
access_log off; #不记录访问图片的日志
}
#JS和CSS缓存时间设置
location ~ .*\.(js|css)?$
{
expires 1h;
}
#日志格式设定
log_format access '$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 access; #对 "/" 启用反向代理
location / {
proxy_pass http://127.0.0.1:88;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#以下是一些反向代理的配置,可选。
proxy_set_header Host $host;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout ; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout ; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout ; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*)
proxy_temp_file_write_size 64k;
#设定缓存文件夹大小,大于这个值,将从upstream服务器传
} #设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
#htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
} #本地动静分离反向代理配置
#所有jsp的页面均交由tomcat或resin处理
location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
}
#所有静态文件由nginx直接读取不经过tomcat或resin
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{ expires 15d; }
location ~ .*.(js|css)?$
{ expires 1h; }
}
}

总结:


nginx的缓存功能有:proxy_cache / fastcgi_cache


proxy_cache的作用是缓存后端服务器的内容,可能是任何内容,包括静态的和动态。
fastcgi_cache的作用是缓存fastcgi生成的内容,很多情况是php生成的动态的内容。
proxy_cache缓存减少了nginx与后端通信的次数,节省了传输时间和后端宽带。
fastcgi_cache缓存减少了nginx与php的通信的次数,更减轻了php和数据库(mysql)的压力。

expire功能优点

(1)expires可以降低网站购买的带宽,节约成本

(2)同时提升用户访问体验

(3)减轻服务的压力,节约服务器成本,是web服务非常重要的功能。

expire功能缺点:

被缓存的页面或数据更新了,用户看到的可能还是旧的内容,反而影响用户体验。

解决办法:第一个缩短缓存时间,例如:1天,但不彻底,除非更新频率大于1天;第二个对缓存的对象改名。

网站不希望被缓存的内容 1)网站流量统计工具2)更新频繁的文件(google的logo)