Nginx的装置步骤
1、装置Nginx依赖库
yum -y install gcc pcre-devel zlib openssl openssl-devel
2、解压Nginx安装包
tar –zxf nginx-1.12.2.tar.gz
3、进入Nginx目录
cd nginx-1.12.2
4、配置文件,生成makefile文件
./configure --prefix=/usr/local/nginx
--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时无需从新编译)
5、编译装置
make && make install
6、 实现装置后进入nginx目录下的sbin目录,启动nginx
./nginx 启动Nginx
./nginx -s reload 重读配置文件
./nginx -s stop 进行Nginx
./nginx -s reopen 重启Nginx
./nginx -s quit 优雅退出
user nobody nobody 配置运行服务器的用户和组(nobody示意所有用户和组都能够启动Nginx)
worker_processes 8 ; 容许生成的worker process数,默认1
error_log logs/error.log info; 配置谬误日志的寄存门路及存储级别(日志级别低到高的程序debug|info|notice|warn|error|crit|alert|emerg,默认error)
pid logs/nginx.pid; 配置PID文件寄存门路
events {
accept_mutex on|off; 设置网络连接的序列化,默认开启(on)状态,给多个Nginx过程接管连贯进行序列化,避免多个过程对连贯争抢multi_accept on|off; 设置同时接管多个网络连接,默认敞开(off)状态,一次只能接管一个新达到的网络连接use method; 配置事件驱动模型,method可抉择的内容有:select、pool、kqueue、epoll、rtsig、/dev/poll、eventportworker_connections 1024; 配置最大连接数(容许每一个worker process同时开启的最大连接数)
}
http {
include file; 配置能够引入的配置文件,反对相对路径default_type application/octet-stream;proxy_cache_path /nginx/cache/first [levels=1:2:1] keys_zone=first:20m [max_size=1g]; 创立缓存,不能定义在server{}上下文中 /nginx/cache/first 在本地创立一个缓存目录 max_size=1g 缓存目录内容超过1G时,nginx会启用cache_manager过程应用最近起码应用算法革除之前的缓存 keys_zone=first:20m 在缓存目录中定义一个20M的名字为first的区域 levels=1:2:1 定义区域中缓存目录的级数及每个级数字符个数(此处定义的缓存目录级数为3级,1级字符个数是1个,2级字符个数是2个,3级字符个数是1个)配置自定义日志格局#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';access_log logs/access.log main; 配置日志文件门路及日志格局sendfile on;tcp_nopush on;keepalive_timeout 65; 设置长连贯超时工夫gzip on|off; 开启敞开压缩性能upstream NAME { 配置后端服务器集群 server 配置后端服务器地址及端口 weight = 配置为权重调度集群 ip_hash; 配置为hash调度集群 least_conn; 配置为起码调度集群 max_fails = 配置最大的健康检查次数 fail_timeout = 配置每次健康检查的超时工夫 back 其它服务器无奈连贯时才转发到此服务器}server { listen 80; 配置监听端口 server_name localhost; 配置主机名 charset koi8-r; location URI {}; 对以后门路及子门路下的所有对象都失效 location = URI {}; 准确匹配指定的门路,不蕴含子门路,因而只对以后资源失效 location ~ URI {}; 模式匹配URI,此处的URI可应用正则表达式,~辨别大小写 location ~* URI {}; 模式匹配URI,此处的URI可应用正则表达式,~*不辨别大小写 location ^~ URI {}; 不应用正则表达式 匹配的优先级程序为location = URI {},location ^~ URI {},location ~ URI {}、location ~* URI {},location URI {} location / { root html; 配置URI的根门路 index index.html index.htm; 配置默认页面 autoindex on; 开启主动索引 stub_status on; deny IP; 回绝IP拜访 allow IP; 容许IP拜访 deny all; 回绝所有IP拜访 auth_basic; 开启基于用户的访问控制 auth_basic_user_file /etc/nginx/.user; 配置管制用户文件的寄存门路 proxy_pass 配置代理的后端服务器 proxy_cache first 启用区域名为first的缓存 proxy_set_header X-Real-IP $remote_addr 配置代理到后端服务器时发送客户端的IP地址 rewrite [模式匹配|判断] 重定向门路 参数 配置URL重定向 判断: if ($request_method = "POST") { proxy_pass } if ($request_uri ~* "/forum") { } 参数: last 本次重写实现之后,重启下一轮查看 break 本次重写实现之后,间接执行后续操作 } location / { 读写拆散配置 proxy_pass http://192.168.3.6/; if ($request_method = "PUT") { 将上传的文件上传到proxy_pass定义的服务器 proxy_pass http://192.168.3.8/ } } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; }}server { HTTPS的配置 listen 443 ssl; server_name localhost; ssl on; 关上ssl性能 ssl_certificate /etc/nginx/ssl/cert.pem; 配置证书寄存地位 ssl_certificate_key cert.key; 配置秘钥寄存地位 ssl_session_cache shared:SSL:1m; 配置会话缓存 ssl_session_timeout 5m; 配置会话超时工夫 ssl_ciphers HIGH:!aNULL:!MD5; 配置加密算法 ssl_prefer_server_ciphers on; 是否容许服务端抉择其偏向的加密算法 location / { root html; index index.html index.htm; }}
}