设为首页 收藏本站
查看: 1140|回复: 0

[经验分享] 编译安装nginx与nginx配置文件

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-12-29 09:11:45 | 显示全部楼层 |阅读模式
一、源码包准备,用的是1.6.2版本,基于centos 6.6 1.png
二、编译     
需要的开发包
yuminstall gcc openssl-devel pcre-devel zlib-devel
创建nginx用户与组,并指明默认shell、不创建家目录      
1
2
3
4
5
6
7
[root@localhostnginx-1.6.2]# groupadd -r nginx
[root@localhostnginx-1.6.2]# useradd -r -g nginx -s /bin/false -M nginx
[iyunv@localhost~]# tar xf nginx-1.6.2.tar.gz
[iyunv@localhost~]# cd nginx-1.6.2
[root@localhostnginx-1.6.2]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx--conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log--http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid--lock-path=/var/lock/nginx.lock --user=nginx --group=nginx--with-http_ssl_module --with-http_flv_module --with-http_stub_status_module--with-http_gzip_static_module--http-client-body-temp-path=/var/tmp/nginx/client--http-proxy-temp-path=/var/tmp/nginx/proxy/--http-fastcgi-temp-path=/var/tmp/nginx/fcgi--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi--http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
[root@localhostnginx-1.6.2]# make
[root@localhostnginx-1.6.2]# make install



各选项的意义:
--prefix= nginx的安装路径,如果没有指定,则为/usr/local/nginx
--sbin-path= nginx的执行文件路径,只能用来安装,如果没有指定则为安装路径下的/sbin/nginx
--conf-path= 没有使用-c 参数指定时nginx的配置文件路径,默认为安装路径下的/conf/nginx/conf
--pid-path= 在nginx.conf中没有设置情形下,nginx.pid文件路径,默认为安装路径下的/logs/nginx.pid
--error-log-path= 在nginx.conf中没有设置的情形下,错误日志的存放路径,默认为安装路径下的/logs/error.log
--http-log-path= 在nginx中没有设置的情形下,访问日志的存放路径,默认为安装路径下的/logs/access.log
--user= 在nginx.conf中没有设置的情况下,设置以谁的身份运行nginx,默认为nobody
--group= 在nginx.conf中没有设置的情况下,设置以哪个用户组的身份运行nginx,默认为nobody
--with-http_ssl_module 启用ssl模块

--with-http_flv_module 启用glv模块
--http-client-body-temp-path=PATH 设置客户端请求临时文件存放目录,默认为安装路径下的/client_body_temp
--http-proxy-temp-path=PATH 设置http代理的临时文件目录,默认为安装路径下的/proxy_temp
--http-fastcgi-temp-path=PATH 设置fastcgi临时文件目录,默认为安装路径下的/fastcgi_temp
--lock-path= 设置nginx的锁文件路径,默认为安装路径下的/nginx/lock
三、为nginx添加服务脚本
编辑/etc/rc.d/init.d/nginx,并添加一下内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/sh
#
# nginx - thisscript starts and stops the nginx daemon
#
#chkconfig:   - 85 15
#description:  Nginx is an HTTP(S) server,HTTP(S) reverse
#               proxy and IMAP/POP3 proxy server
# processname:nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
  
# Sourcefunction library.
./etc/rc.d/init.d/functions
  
# Sourcenetworking configuration.
./etc/sysconfig/network
  
# Check thatnetworking is up.
["$NETWORKING" = "no" ] && exit 0
  
nginx="/usr/sbin/nginx"
prog=$(basename$nginx)
  
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
  
[ -f/etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
  
lockfile=/var/lock/subsys/nginx
  
make_dirs() {
   # make required directories
   user=`nginx -V 2>&1 | grep"configure arguments:" | sed 's/[^*]*--user=([^ ]*).*//g' -`
   options=`$nginx -V 2>&1 | grep'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'`]; then
           value=`echo $opt | cut -d"=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating"$value
               mkdir -p $value && chown-R $user $value
           fi
       fi
   done
}
  
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
  
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f$lockfile
    return $retval
}
  
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
  
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
  
force_reload() {
    restart
}
  
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
  
rh_status() {
    status $prog
}
  
rh_status_q() {
    rh_status >/dev/null 2>&1
}
  
case"$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)        echo $"Usage: $0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac



为脚本赋予执行权限:

1
[root@localhostnginx-1.6.2]# chmod +x /etc/rc.d/init.d/nginx



添加至服务管理列表,并能开机启动
1
2
3
4
[root@localhostnginx-1.6.2]# chkconfig --add nginx
[root@localhostnginx-1.6.2]# chkconfig  nginx on
[root@localhostnginx-1.6.2]# chkconfig --list nginx
nginx              0:off1:off2:on3:on4:on5:on6:off



四、启动测试

2.png
已成功启动
五、nginx的工作模式
nginx启动时会生成一个master主进程,主进程会启动多个worker线程,负责处理用户请求,master负责监控worker进程数,是否启动的够数目,是否工作正常等,以及管理其他负责缓存的进程,master以系统用户运行,而worker线程是以普通进程运行,本身worker只能处理最简单的http请求,而其他的高级功能都是有模块处理完成的,这些模块都有各自的作用,以流水线的方式处理用户的请求,worker负责分配哪些模块去处理用户的请求,master负责装载配置文件,如果配置文件更改,master会重新载入,已经启动的worker会继续使用旧的配置文件,当worker请求处理完毕时,会被杀死,master会以新的配置文件重新启动worker线程,此时新的配置文件便会生效,从而不会影响nginx运行
六、nginx配置文件
nginx的配置文件的自定义程度非常高
main配置段
1
2
http{
}



配置指令要以分号结尾,语法格式
directive value1[value2,...];
指令值(后可跟多个值);
支持使用变量
模块内置变量
  自定义变量
set var_name value

主配置段的指令的类别
用于调试、定位问题
用于正常运行必备的配置
优化性能的配置
事件相关的配置
正常运行的必备配置:
1、user USERNAME [GROUPNAME]
指定运行worker进程的用户和组
2、pid/path/to/somewhere
指定pid文件路径
3、worker_rlimit_nofile #:
一个worker文件所能够打开的最大文件句柄数
4、worker_rlimit_sigpending:用于设定每个用户能够发往worker进程的信号的数量
优化性能相关的配置
1、worker_processes # ;启动的worker线程的个数,通常为物理cpu核心的个数或-1,如图当设置此值为2则有两个worker进程,而且从此图可以看出master是以root用户身份运行的,而worker进程是以nginx运行的。
4.png
2、worker_cpu_affinity cpumask...
绑定worker进程至指定的cpu上
cpumask
0001
0010
0100
1000
worker_cpu_affinity 00000001 00000010 00000100
3、worker_priority nice;
以指定nice至运行程序   -20到19
ps axo pid,command,nice命令可查看
事件相关的配置
1、access_mutex(互斥)[on|off],是否打开nginx的负载均衡锁
内部调用用户请求至各worker时用的负载均衡锁;打开时表示能够让多个worker轮流的序列化的响应新请求
2、lock_file (和脚本中的文件无关)
3、accept_mutex_delay #ms;
一个worker进程等待锁文件
4、use [epoll|rgsig|select|poll]
指明使用那个模型
5、worker_connections #
每个woreker进程所能够响应的最大并发请求数
用户用于调试、定位问题
1、daemon [off|on]
是否以守护进程方式启动nginx,正常环境中为on
2、master_process [on|off]
是否以master/worker模型来运行nginx
3、error_log /path/to/somewhere level;
错误日志文件及其级别;处于调试的目的,可以使用debug级别,但此级别只有在编译nginx时使用了--with-debug选项才有效
七、nginx的模块化配置
ginx_http_core_module
配置框架
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
http{
    upstream{
    ....
    }
    server{
        Listenip:port
        # 虚拟主机
        location/url{
            if ...{
            ....
            }
        root"本地文件路径".....
        }
  
    }
    server{
        #虚拟主机
    }
}



虚拟主机相关的配置
1,server{ }
定义一个虚拟主机
2、listen ip:port
监听的端口;
5.png
backlog=number
tcp协议backlog队列的大小。默认为-1,表示不启用
rcvbuf=size:设置监听的句柄的SO_RECVBUF参数
sendbuf=size设置监听的句柄的SO_SENTBUF参数
listen 172.16.100.8:8080
3、server_name
后可跟多个主机名,名称可以使用通配符和正则表达式(~)
1、先做精确匹配:www.stu30.com,只有访问www.stu30.com才会匹配此主机
2、左侧通配符匹配:*.magedu.com,没有被精确匹配匹配到,则会
3、右侧通配符匹配:www.*;
4、正则表达式匹配:~^.*.magedu.com$
4、location [=|~|~*|^~] /uri { ... }
loclation @name
功能:允许根据用户请求的URL来匹配定义的各location,匹配到时,此请求将被响应的location块中的配置所处理;
1
2
3
4
5
server {
location /images { }
location /bbs { }
location / { }
}



=:精确匹配检查
~:正则表达式模式匹配,区分字符大小写
~*:正则表达式匹配,不区分字符大小写
^~:URL前半部分匹配,不检查正则表达式
6.png
匹配优先级:(=)>(^~) >(~  ~*)>(不带任何符号)
5、root
设置web的资源路径映射,用来指明请求的URL所对应的文档的目录路径
1
2
3
location /image/{
root "/web/imgs";
}



访问.com/image 对应的文件路径为/web/imgs/image

6、alias path
用于location配置段。定义路径别名
1
2
3
location /image/{
alias "/web/imgs";
}



访问.com/image 对应的文件路径为/web/imgs
7、index file;
默认主页面
index index.html:
8、error_page code [...] [=code]URI
根据http的状态码重定向错误页面
error_page 404 /404.html
=code,以指定的响应码进行响应
=:以新资源的响应码为响应码
网络连接相关的配置
1、keepalive_timeouttime
保持连接的超时时长,默认为75秒
2、keepalive——requests #
在一次保持连接上允许承载的最大资源请求数
3、keepalive_disable[msie6|sarari|none]
为指定类型的浏览器禁用保持连接
4、tcp_nodelayon|off
对保持连接是否使用TCP_NODELAY选项
5、client_header_timetime ;
读取http请求报文首部的超时时长
6、client_body_timeouttime
读取http请求报文body部分的超时时长
7、send_timeouttime
发送响应报文的超时时长
对客户端请求进行限制
1、limit_except METHOD {...}
指定对范围之外的其他方法的访问控制;
1
2
3
limit_exceptGET {
allow 172.16.0.0/16;
deny all}




2、client_body_max_size SIZE
限制请求报文中的body部分的上限,通过检测请求报文首部中的Content Length来判定
3、limit_rate speed
限制客户端每秒钟传输的字节数,默认为0表示不限制
实现对内存和磁盘资源进行分配
1、client_body_in_file_only on|clean|off
http的请求报文的body是否可暂时存储在磁盘
on 允许,即便请求结束也不会删除暂存内容
clean会删除
off不允许暂存
2、client_body_buffer-size size
暂存大小
3、client_body_temp_path DIR [level1[level2[level3]]]
4、client_header_buffer_size size
请求报文首部分配的内存空间大小
MIME类型相关的配置
1、types
如何判断某种扩展名类型
定义MIME types至文件的扩展名
types{
text/html .html;
image/jieg .jpg;
}
2、defaults_type MIME=TYPE
文件操作优化相关的配置
1、sendfile onoff
2、aio:是否启用异步IO
3、directio size | off是否启用直接IO 安全但是性能差
是否使用O_DIRRCT选项去请求读取文件,与sendfile功能互斥
4、open_file_cache max =N[inactive=time]| off
nginx可以缓存一下三种信息
1.文件句柄、文件大小和最近一次修改时间
2、打开目录的目录结构
3、没有找到的或者没有权限操作的文件的相关信息
max=N 表示可缓存的最大条目上限,一旦达到上限,则会使用LRU 从缓存中删除最近最少使用的条目
inactive=time 在inactive指定的时长内没有被访问过的缓存条目就会淘汰
5、open_fule_errors on|off
是否缓存在文件缓存打开文件时出现找不到路径,没有全乡等的错误信息
6、open_file_cache_min
基于IP的访问控制
同样可以使用172.16.0.0/16等网络地址,all表示拒绝所有,请求从上往下匹配,匹配到则执行拒绝或允许,没 匹配到则进入下一条开始匹配
7.png
此时重启服务便会被拒绝了        
8.png
而允许的则可以访问
9.png
基于用户的访问控制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[iyunv@localhost~]# htpasswd -c -m /etc/nginx/.htpasswd stu30
New password:
Re-type new password:
Adding password for user stu30
[iyunv@localhost ~]# mkdir/nginx/htdocs/admin -pv
mkdir: created directory `/nginx'
mkdir: created directory `/nginx/htdocs'
mkdir: created directory`/nginx/htdocs/admin'
[iyunv@localhost ~]# !ser
service nginx restart
nginx: the configuration file/etc/nginx/nginx.conf syntax is ok
nginx: configuration file/etc/nginx/nginx.conf test is successful
Stopping nginx:                                           [  OK  ]
Starting nginx:                                             [ OK  ]



访问结果:需要输入密码,才能访问
10.png
11.png
基于gzip实现响应报文压缩 12.png
13.png
定制请求头部信息
14.png
测试:
15.png
URL重写
情景假设:论坛路径由bbs换为forum,如果不进行URL重写,会无法访问,通过URL重写将bbs换为forum即可正常访问
首先创建bbs目录      

1
[iyunv@localhost~]# mkdir /nginx/htdocs/bbs



创建主页文件

1
2
[iyunv@localhost~]# vim /nginx/htdocs/bbs/index.html
BBS



访问测试:
16.png
将bbs目录修改为forum
1
[iyunv@localhost~]# mv /nginx/htdocs/bbs/ /nginx/htdocs/forum



访问测试:
17.png 18.png
访问测试
19.png
而URL重写还有标识位
没有添加标识位的请求结果,服务器会自己将每次请求进行检查,最终返回客户端最终结果
20.png
添加标识位redirect
21.png
此时服务器会先返回一个302的临时重定向,客户端自己再次发送请求重定向之后的结果 22.png





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-39516-1-1.html 上篇帖子: Nginx主配置文件详解 下篇帖子: Nginx虚拟主机配置 配置文件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表