Nginx源码编译安装选项及配置文件部分参数解读

依赖包安装:
yum -y install zlib zlib-devel openssl openssl–devel pcre pcre-devel

./configure 参数(安装时可选):
–user=www \ #worker进程运行用户
–group=www \ #worker进程运行的组
–prefix=/usr/ \ #Nginx安装的根路径,所有其他的路径都要依赖于改选项
–conf-path=/etc/nginx/nginx.conf \ #如果在命令行没有指定配置文件,那么将会通过这里指定的路径,Nginx将会去那里查找它的配置文件
–sbin-path=/usr/sbin/nginx \ #指定Nginx二进制文件的路径。如果没有指定,那么这个路径会依赖于–prefix选项
–error-log-path=/var/log/nginx/nginx_error.log \ #指定错误文件的路径,Nginx将会往其中写入错误日志文件,除非有其他配置
–http-log-path=/var/log/nginx/nginx_access.log \ #http访问日志的默认路径
–pid-path=/usr/local/nginx/run/nginx.pid \ #指定的文件将会写入Nginx master进程的pid,通常在/var/run下
–lock-path=/usr/local/nginx/lock/nginx \ #共享存储器互斥锁文件的路径
–with-http_ssl_module \ #启用 ngx_http_ssl_module 支持(使支持 https 请求,需已安装openssl)
–with-http_realip_module \ #启用 ngx_http_realip_module 支持(这个模块允许从请求标头更改客户端的 IP 地址值,默认为关)
–with-http_addition_module \ #启用 ngx_http_addition_module 支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求)
–with-http_sub_module \ #启用 ngx_http_sub_module 支持(允许用一些其他文本替换nginx 响应中的一些文本)
–with-http_dav_module \ #启用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:创建集合,COPY 和 MOVE 方法)默认情况下为关闭,需编译开启
–with-http_flv_module \ #启用 ngx_http_flv_module 支持(提供寻求内存使用基于时间的偏移量文件)
–with-http_gzip_static_module \ #启用 ngx_http_gzip_static_module 支持(在线实时压缩输出数据流)
–with-http_stub_status_module \ #启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)
–with-http_perl_module \ #Nginx配置能够扩展使用Perl代码。这个选项启用这个模块(然而使用这个模块会降低性能)
–with-mail \ #该选项用于启用mail模块,该模块默认没有被激活
–with-mail_ssl_module \ #为了代理任何一种类型的使用SSL/TLS的mail,激活该模块
–with-pcre \
–http-client-body-temp-path=/var/tmp/nginx/client/ \ #从客户端收到请求后,该选项设置的目录用于作为请求体零食存放的目录,如果WebDAV模块启用,那么推荐设置该路径为同一文件系统上的目录作为最终的目的地
–http-proxy-temp-path=/var/tmp/nginx/proxy \ #在使用代理后,通过该选项设置存放临时文件路径
–http-fastcgi-temp-path=/var/tmp/nginx/fcgi \ #设置FastCGI临时文件的目录
–http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ #设置uWSGI临时文件的目录
–http-scgi-temp-path=/var/tmp/nginx/scgi #设置SCGI临时文件的目录

配置文件nginx.conf解读
user www; #运行用户
worker_processes 1; #启动进程,通常设置成和cpu的数量相等
error_log /var/log/nginx/error.log; #全局错误日志
pid /usr/local/nginx/run/nginx.pid; #pid文件
log_format access ‘ r e m o t e a d d r remote_user [ t i m e l o c a l ] " request” ’
s t a t u s body_bytes_sent “ h t t p r e f e r e r http_user_agent" http_x_forwarded_for’;   日志格式,以下具体介绍变量 remote_addr 与 http_x_forwarded_for    #用以记录客户端的 ip 地址; remote_user #用来记录客户端用户名称;
time_local    #用来记录访问时间与时区; request #用来记录请求的 url 与 http 协议;
status    #用来记录请求状态;成功是 200; body_bytes_s ent #记录发送给客户端文件主体内容大小;
$http_referer #用来记录从那个页面链接访问过来的;

工作模式及连接数上限
events {
use epoll; #epoll 是多路复用 IO(I/O Multiplexing)中的一种方式,但是仅用于 linux2.6以上内核,可以大大提高 nginx 的性能
worker_connections 1024; #单个后台 worker process 进程的最大并发链接数
multi_accept on;
}

设定 http 服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定 mime 类型,类型由 mime.type 文件定义
include /etc/nginx/mime.types;
default_type application/octet-stream;
#设定日志格式
access_log /usr/local/nginx/logs/access.log;
#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
#必须设为 on,如果用来进行下载等应用磁盘 IO 重负载应用,可设置为 off,以平衡磁盘与网络 I/O 处理速度,降低系统的 uptime.
sendfile on;
#tcp_nopush on;
#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
#开启 gzip 压缩
gzip on;
gzip_disable “MSIE [1-6].(?!.*SV1)”;
#设定请求缓冲
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
#设定负载均衡的服务器列表
upstream mysvr {
#weigth 参数表示权值,权值越高被分配到的几率越大
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
}
server {
#侦听 80 端口
listen 80;
#定义使用 www.xx.com 访问
server_name www.xxx.com;
#设定本虚拟主机的访问日志
access_log /var/log/nginx/www.xxx.com_access.log access;
#默认请求
location / {
root /var/www/html/; #定义服务器的默认网站根目录位置
index index.php index.html index.htm; #定义首页索引文件的名称
}
# 定义错误提示页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /root;
}
#静态文件,nginx 自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/htdocs;
#过期 30 天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
expires 30d;
}
#PHP 脚本请求全部转发到 FastCGI 处理. 使用 FastCGI 默认配置.
location ~ .php {                  root /root;                  fastcgi_pass 127.0.0.1:9000;                  fastcgi_index index.php;                  fastcgi_param SCRIPT_FILENAME/home/www/www fastcgi_script_name;
include fastcgi_params;
}
#设定查看 Nginx 状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic “NginxStatus”;
auth_basic_user_file conf/htpasswd;
}
#禁止访问 .htxxx 文件
location ~ /.ht {
allow host;
deny all;
}
}
}

Nginx常用配置实战
基于域名的虚拟主机
server {
listen 80;
server_name www.Daniel.org Daniel.org;
access_log /var/log/nginx/Daniel/Daniel_access.log main;
error_log /var/log/nginx/Daniel/Daniel_error.log crit;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
多域名就多个server段

基于端口的只需要将listen修改端口即可,server_name也要修改成IP

别名:
上面例子中,server_name中有两个域名,这个就是别名

nginx状态信息配置
server{
listen 80;
server_name status.Daniel.org;
location / {
stub_status on;
access_log off;
}
}
浏览器访问status.Daniel.org:

Active connections: 3 #正在处理的活动连接数
server accepts handled requests #server表示nginx启动到现在共处理了几个连接,accepts表示nginx启动到现在共成功创建了几次连接,请求丢失数据=(握手数-连接数),可以看出,本次状态显示没有丢失请求,handled requests表示总共处理了几次请求
9 9 192
Reading: 0 Writing: 1 Waiting: 2
Reading:nginx读取客户端的Header信息数
Writing:nginx返回给客户端的Header信息数
Waiting:nginx已经处理完正在等候下一次请求指令的驻留连接,开启keep-alive的情况下,这个值等于active-(reading + writing)

访问控制权限实战
server {
listen 80;
server_name www.etiantian.org etiantian.org;
access_log /var/log/nginx/etiantian/etiantian_access.log main;
error_log /var/log/nginx/etiantian/etiantian_error.log crit;
location / {
root html/www;
index index.html index.htm;
deny 172.16.50.173; #拒绝172.16.50.173
allow all; #允许全部,意思就是允许除了172.16.50.173的其他全部IP访问
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

server{
    listen 80;
    server_name status.Daniel.org;
    location / {
        stub_status on;
        access_log off;
        allow 172.16.50.173; #只允许172.16.50.173,其他全部拒绝
        deny all;

    }
}

也可以用网段表示:172.16.50.0/24

安装好nginx后,其他机器出现无法访问页面时,可能是防火墙的原因:
停用防火墙或防火墙开放80端口
CentOS7系统下,停用防火墙: systemctl stop firewalld

猜你喜欢

转载自blog.csdn.net/shaopeng1942/article/details/81701210