nginx服务器安装及配置文件详解

发布时间:2024-12-23 点击:91
转载自sean
nginx在工作中已经有好几个环境在使用了,每次都是重新去网上扒博客,各种编译配置,今天自己也整理一份安装文档和nginx.conf配置选项的说明,留作以后参考。像负载均衡配置(包括健康检查)、缓存(包括清空缓存)配置实例,请参考 http://seanlook.com/2015/05/17/nginx-install-and-config ,ssl加密请参考 http://seanlook.com/2015/05/28/nginx-ssl/ 。
1. 安装nginx 1.1 选择稳定版本
我们编译安装nginx来定制自己的模块,机器centos 6.2 x86_64。首先安装缺少的依赖包:
yum -y install gcc gcc-c make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
这些软件包如果yum上没有的话可以下载源码来编译安装,只是要注意编译时默认安装的目录,确保下面在安装nginx时能够找到这些动态库文件(ldconfig)。
从 http://nginx.org/en/download.html 下载稳定版nginx-1.6.3.tar.gz到/usr/local/src下解压。
为了后续准备我们另外下载2个插件模块:nginx_upstream_check_module-0.3.0.tar.gz —— 检查后端服务器的状态,nginx-goodies-nginx-sticky-module-ng-bd312d586752.tar.gz(建议在/usr/local/src*下解压后将目录重命名为nginx-sticky-module-ng-1.2.5) —— 后端做负载均衡解决session sticky问题(与upstream_check模块结合使用需要另外打补丁,请参考nginx负载均衡配置实战)。
请注意插件与nginx的版本兼容问题,一般插件越新越好,nginx不用追新,稳定第一。nginx-1.4.7,nginx-sticky-module-1.1,nginx_upstream_check_module-0.2.0,这个搭配也没问题。sticky-1.1与nginx-1.6版本由于更新没跟上编译出错。(可以直接使用tengine,默认就包括了这些模块)
[root@cachets nginx-1.6.3]# pwd /usr/local/src/nginx-1.6.3 [root@cachets nginx-1.6.3]# ./configure –prefix=/usr/local/nginx-1.6 –with-pcre > –with-http_stub_status_module –with-http_ssl_module > –with-http_gzip_static_module –with-http_realip_module > –add-module=../nginx_upstream_check_module-0.3.0 [root@cachets nginx-1.6.3]# make && make install 1.2 常用编译选项说明
nginx大部分常用模块,编译时./configure –help以–without开头的都默认安装。
–prefix=path : 指定nginx的安装目录。默认 /usr/local/nginx
–conf-path=path : 设置nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf
–user=name: 设置nginx工作进程的用户。安装完成后,可以随时在nginx.conf配置文件更改user指令。默认的用户名是nobody。–group=name类似
–with-pcre : 设置pcre库的源码路径,如果已通过yum方式安装,使用–with-pcre自动找到库文件。使用–with-pcre=path时,需要从pcre网站下载pcre库的源码(版本4.4 – 8.30)并解压,剩下的就交给nginx的./configure和make来完成。perl正则表达式使用在location指令和 ngx_http_rewrite_module模块中。
–with-zlib=path : 指定 zlib(版本1.1.3 – 1.2.5)的源码解压目录。在默认就启用的网络传输压缩模块ngx_http_gzip_module时需要使用zlib 。
–with-http_ssl_module : 使用https协议模块。默认情况下,该模块没有被构建。前提是openssl与openssl-devel已安装
–with-http_stub_status_module : 用来监控 nginx 的当前状态
–with-http_realip_module : 通过这个模块允许我们改变客户端请求头中客户端ip地址值(例如x-real-ip 或 x-forwarded-for),意义在于能够使得后台服务器记录原始客户端的ip地址
–add-module=path : 添加第三方外部模块,如nginx-sticky-module-ng或缓存模块。每次添加新的模块都要重新编译(tengine可以在新加入module时无需重新编译)
再提供一种编译方案:
./configure > –prefix=/usr > –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_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 > –with-pcre=../pcre-7.8 > –with-zlib=../zlib-1.2.3 1.3 启动关闭nginx
检查配置文件是否正确
# /usr/local/nginx-1.6/sbin/nginx -t # ./sbin/nginx -v # 可以看到编译选项 ## 启动、关闭 # ./sbin/nginx # 默认配置文件 conf/nginx.conf,-c 指定 # ./sbin/nginx -s stop 或 pkill nginx ## 重启,不会改变启动时指定的配置文件 # ./sbin/nginx -s reload
或 kill -hup cat /usr/local/nginx-1.6/logs/nginx.pid
当然也可以将 nginx 作为系统服务管理,下载 nginx 到/etc/init.d/,修改里面的路径然后赋予可执行权限。
# service nginx {start|stop|status|restart|reload|configtest} 1.4 yum安装
yum安装rpm包会比编译安装简单很多,默认会安装许多模块,但缺点是如果你想以后安装第三方模块那就没办法了。
# vi /etc/yum.repo.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1
1核2g云服务器能挂网页游戏吗
北京云端服务器租用
浙江规划打造10大标志性产业链!到2025年总产值将突破6万亿元
如何进行搜索功能设计?
从杨帆老师网站策划博客看Google Sitelinks
提升网站建设效果从用户和竞争对手方面考虑
河北服务器机柜云主机报价
企业域名申请技巧 企业申请域名要注意什么