Nginx简介及配置文件详解
http://blog.csdn.net/hzsunshine/article/details/63687054
一 Nginx简介
#安装部署nginx所用到的安装工具和相关库#默认安装的http_rewrite_module(使用正则对请求重写)需pcre库#默认安装的httP_gzip_module(Gzip压缩)需zlib库#安装http_ssl_module(HTTPS/SLL)需openssl库yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel#下载nginx源码包,并解压wget http://nginx.org/download/nginx-1.10.3.tar.gztar -zxvf nginx-1.10.3.tar.gzcd nginx-1.10.3#设置参数 参数具体参考《Nginx编译参数》./configure \--prefix=/usr/local/nginx \--with-http_ssl_module#编译并安装- make && make install
#Nginx的worker进程运行用户以及用户组#user nobody nobody;#Nginx开启的进程数;#worker_processes auto;#以下参数指定了哪个cpu分配给哪个进程,一般来说不用特殊指定。如果一定要设的话,用0和1指定分配方式.#这样设就是给1-4个进程分配单独的核来运行,出现第5个进程是就是随机分配了。eg:#worker_processes 4 #4核CPU#worker_cpu_affinity 0001 0010 0100 1000#定义全局错误日志定义类型,[debug|info|notice|warn|crit]#error_log logs/error.log info;#指定进程ID存储文件位置#pid logs/nginx.pid;#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。- #vim /etc/security/limits.conf
- # * soft nproc 65535
- # * hard nproc 65535
- # * soft nofile 65535
# * hard nofile 65535worker_rlimit_nofile 65535;
events {#use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。use epoll;#每个进程可以处理的最大连接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections。理论值:worker_rlimit_nofile/worker_processes#注意:最大客户数也由系统的可用socket连接数限制(~ 64K),所以设置不切实际的高没什么好处;#worker工作方式:串行(一定程度降低负载,但服务器吞吐量大时,关闭使用并行方式)#multi_accept on;}
#文件扩展名与文件类型映射表include mime.types;#默认文件类型default_type application/octet-stream;- #日志相关定义
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#定义日志的格式。后面定义要输出的内容。#1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;#2.$remote_user :用来记录客户端用户名称;#3.$time_local :用来记录访问时间与时区;#4.$request :用来记录请求的url与http协议;#5.$status :用来记录请求状态;#6.$body_bytes_sent :记录发送给客户端文件主体内容大小;#7.$http_referer :用来记录从那个页面链接访问过来的;#8.$http_user_agent :记录客户端浏览器的相关信息#连接日志的路径,指定的日志格式放在最后。#access_log logs/access.log main;#只记录更为严重的错误日志,减少IO压力error_log logs/error.log crit;#关闭日志#access_log off;#默认编码#charset utf-8;#服务器名字的hash表大小server_names_hash_bucket_size 128;#客户端请求单个文件的最大字节数client_max_body_size 8m;#指定来自客户端请求头的hearerbuffer大小client_header_buffer_size 32k;#指定客户端请求中较大的消息头的缓存最大数量和大小。large_client_header_buffers 464k;#开启高效传输模式。sendfile on;#防止网络阻塞tcp_nopush on;tcp_nodelay on;#客户端连接超时时间,单位是秒;#客户端请求头读取超时时间client_header_timeout 10;
#设置客户端请求主体读取超时时间
client_body_timeout 10;
#响应客户端超时时间
send_timeout 10;#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 64k;fastcgi_buffers 464k;fastcgi_busy_buffers_size 128k;fastcgi_temp_file_write_size 128k;#gzip模块设置#开启gzip压缩输出gzip on;#最小压缩文件大小gzip_min_length 1k;#压缩缓冲区gzip_buffers 416k;#压缩版本(默认1.1,前端如果是squid2.5请使用1.0)gzip_http_version 1.0;#压缩等级 1-9 等级越高,压缩效果越好,节约宽带,但CPU消耗大gzip_comp_level 2;#压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。gzip_types text/plain application/x-javascript text/css application/xml;#前端缓存服务器缓存经过压缩的页面
gzip_vary on;
#虚拟主机定义server {#监听端口
listen 80;#访问域名
server_name localhost;#编码格式,若网页格式与此不同,将被自动转码
#charset koi8-r;#虚拟主机访问日志定义
#access_log logs/host.access.log main;#对URL进行匹配
location /{#访问路径,可相对也可绝对路径
root html;#首页文件。以下按顺序匹配
index index.html index.htm;}#错误信息返回页面#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#/50x.html;location =/50x.html {root html;}#访问URL以.php结尾则自动转交给127.0.0.1
# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}#php脚本请求全部转发给FastCGI处理# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##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;#}#禁止访问.ht页面 (需ngx_http_access_module模块)
# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}#HTTPS虚拟主机定义# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate 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;# }#}
#Nginx运行状态,StubStatus模块获取Nginx自启动的工作状态(编译时要开启对应功能)#location /NginxStatus {# #启用StubStatus的工作访问状态# stub_status on;# #指定StubStaus模块的访问日志文件- # access_log logs/Nginxstatus.log;
- # #Nginx认证机制(需Apache的htpasswd命令生成)
- # #auth_basic "NginxStatus";
- # #用来认证的密码文件
- # #auth_basic_user_file ../htpasswd;
#}- 访问:http://IP/NginxStatus(测试就不加密码验证相关)
- #以下配置追加在HTTP的全局变量中
- #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_connect_timeout 5;- #后端服务器数据回传时间(代理发送超时)
proxy_send_timeout 5;- #连接成功后,后端服务器响应时间(代理接收超时)
proxy_read_timeout 60;- #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffer_size 16k;- #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_buffers 432k;- #高负荷下缓冲大小(proxy_buffers*2)
proxy_busy_buffers_size 64k;- #设定缓存文件夹大小,大于这个值,将从upstream服务器传
proxy_temp_file_write_size 64k;- #反向代理缓存目录
- proxy_cache_path /data/proxy/cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=1g;
#levels=1:2 设置目录深度,第一层目录是1个字符,第2层是2个字符
#keys_zone:设置web缓存名称和内存缓存空间大小
#inactive:自动清除缓存文件时间。
#max_size:硬盘空间最大可使用值。
#指定临时缓存文件的存储路径(路径需和上面路径在同一分区)proxy_temp_path/data/proxy/temp#服务配置server {#侦听的80端口listen 80;server_name localhost;location /{#反向代理缓存设置命令(proxy_cache zone|off,默认关闭所以要设置)
proxy_cache cache_one;
#对不同的状态码缓存不同时间
proxy_cache_valid 200 304 12h;
#设置以什么样参数获取缓存文件名
proxy_cache_key $host$uri$is_args$args;#后7端的Web服务器可以通过X-Forwarded-For获取用户真实IPproxy_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://IP;#文件过期时间控制
expires 1d;}#配置手动清楚缓存(实现此功能需第三方模块 ngx_cache_purge)
#http://www.123.com/2017/0316/17.html访问#http://www.123.com/purge/2017/0316/17.html清楚URL缓存
- location ~ /purge(/.*) {
- allow 127.0.0.1;
- deny all;
- proxy_cache_purge cache_one $host$1$is_args$args;
- }
- #设置扩展名以.jsp、.php、.jspx结尾的动态应用程序不做缓存
- location ~.*\.(jsp|php|jspx)?$ {
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://http://IP;
- }
#负载均衡服务器池upstream my_server_pool {#调度算法
#1.轮循(默认)(weight轮循权值)
#2.ip_hash:根据每个请求访问IP的hash结果分配。(会话保持)
#3.fair:根据后端服务器响应时间最短请求。(upstream_fair模块)
#4.url_hash:根据访问的url的hash结果分配。(需hash软件包)#参数:
#down:表示不参与负载均衡
#backup:备份服务器
#max_fails:允许最大请求错误次数
#fail_timeout:请求失败后暂停服务时间。- server 192.168.1.109:80 weight=1 max_fails=2 fail_timeout=30;
server 192.168.1.108:80 weight=2 max_fails=2 fail_timeout=30;}#负载均衡调用server {...location / {proxy_pass http://my_server_pool;}
}
#根据不同的浏览器URL重写if($http_user_agent ~Firefox){rewrite ^(.*)$ /firefox/$1 break;}if($http_user_agent ~ MSIE){rewrite ^(.*)$ /msie/$1 break;}#实现域名跳转location /{rewrite ^/(.*)$ https://web8.example.com$1 permanent;}
#限制IP访问location /{deny 192.168.0.2;allow 192.168.0.0/24;allow 192.168.1.1;deny all;}
- #启动nginx
- nginx
- #关闭nginx
- nginx -s stop
- #平滑重启
- kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
#!/bin/bash#chkconfig: 2345 80 90
#description:auto_run
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATH# Check if user is rootif[ $(id -u)!="0"];thenecho "Error: You must be root to run this script!\n"exit 1fiNGINXDAEMON=/usr/local/nginx/sbin/nginxPIDFILE=/usr/local/nginx/logs/nginx.pidfunction_start(){echo -en "\033[32;49;1mStarting nginx......\n"echo -en "\033[39;49;0m"if[-f $PIDFILE ];thenprintf "Nginx is runing!\n"exit 1else$NGINXDAEMONprintf "Nginx is the successful start!\n"fi}function_stop(){echo -en "\033[32;49;1mStoping nginx......\n"echo -en "\033[39;49;0m"if[-f $PIDFILE ];thenkill `cat $PIDFILE`printf "Nginx program is stoped\n"elseprintf "Nginx program is not runing!\n"fi}function_reload(){echo -en "\033[32;49;1mReload nginx......\n"echo -en "\033[39;49;0m"- function_stop
function_start }function_restart(){echo -en "\033[32;49;1mRestart nginx......\n"echo -en "\033[39;49;0m"printf "Reload Nginx configure...\n"$NGINXDAEMON -tkill -HUP `cat $PIDFILE`printf "Nginx program is reloding!\n"}function_kill(){killall nginx}function_status(){if! ps -ef|grep -v grep|grep 'nginx:'>/dev/null 2>&1thenprintf "Nginx is down!!!\n"elseprintf "Nginx is running now!\n"fi}if["$1"="start"];thenfunction_startelif["$1"="stop"];thenfunction_stopelif["$1"="reload"];thenfunction_reloadelif["$1"="restart"];thenfunction_restartelif["$1"="kill"];thenfunction_killelif["$1"="status"];thenfunction_statuselseecho -en "\033[32;49;1m Usage: nginx {start|stop|reload|restart|kill|status}\n"echo -en "\033[39;49;0m"fi
Nginx简介及配置文件详解的更多相关文章
- 【转】nginx服务器安装及配置文件详解
原文:http://seanlook.com/2015/05/17/nginx-install-and-config/ nginx服务器安装及配置文件详解 nginx在工作中已经有好几个环境在使用了, ...
- [转帖]nginx服务器安装及配置文件详解
nginx服务器安装及配置文件详解 http://seanlook.com/2015/05/17/nginx-install-and-config/ 发表于 2015-05-17 | 更新于: 2 ...
- ubuntu nginx 安装以及配置文件详解
1.到nginx官网下载源码包.最好下载稳定版本,nginx官网http://www.nginx.org/ 2.安装nginx依赖包运行命令: sudo apt-get install libssl- ...
- nginx安装及配置文件详解
一)nginx安装及模块讲解 1.1.nginx安装步骤 mkdir /soft wget http://nginx.org/download/nginx-1.12.0.tar.gz tar zxf ...
- nginx服务器安装及配置文件详解
nginx在工作中已经有好几个环境在使用了,每次都是重新去网上扒博客,各种编译配置,今天自己也整理一份安装文档和nginx.conf配置选项的说明,留作以后参考.像负载均衡配置(包括健康检查).缓存( ...
- Nginx 服务器安装及配置文件详解
原文出处: Sean Chow(@SeanLoook) 欢迎分享原创到伯乐头条 Nginx 在工作中已经有好几个环境在使用了,每次都是重新去网上找博客,各种编译配置,今天自己也整理一份安装文档和 ...
- 【转】Nginx 服务器安装及配置文件详解
1. 安装nginx 1.1 选择稳定版本 我们编译安装nginx来定制自己的模块,机器CentOS 6.2 x86_64.首先安装缺少的依赖包: # yum -y install gcc gcc-c ...
- <转载> nginx服务器安装及配置文件详解 https://segmentfault.com/a/1190000002797601
nginx在工作中已经有好几个环境在使用了,每次都是重新去网上扒博客,各种编译配置,今天自己也整理一份安装文档和nginx.conf配置选项的说明,留作以后参考.像负载均衡配置(包括健康检查).缓存( ...
- nginx默认的配置文件详解
# For more information on configuration, see:# * Official English Documentation: http://nginx.org/en ...
随机推荐
- WPF:事件委托对于不同界面间通信的应用
界面1内设定点击事件,生成Path用事件传出public partial class TemplateWindow : Window { internal delegate v ...
- Robotframework获取移动端toast问题
背景: 在做移动端自动化测试的时候,经常会遇到一个问题就是获取toast提示问题,如果需要解决这个问题需要重新处理,不能按照正常的逻辑,使用robotframework自带的关键字进行获取,需要重新考 ...
- NLP(十四)自制序列标注平台
背景介绍 在平时的NLP任务中,我们经常用到命名实体识别(NER),常用的识别实体类型为人名.地名.组织机构名,但是我们往往也会有识别其它实体的需求,比如时间.品牌名等.在利用算法做实体识别的时候 ...
- 自己学习并保存的一些shell命令
摘要: 在学习过程中,不免会遇到有些命令,那种需要的,但是你没有掌握的命令.为了节省时间,担心忘记这些,特开辟这个随笔,随时记录用到的一些命令.那么常用的不提了,自己去收集吧~ 1.文件按日期排序 应 ...
- 分布式系统的一致性级别划分及Zookeeper一致性级别分析
最近在研究分布式系统的一些理论概念,例如关于分布式系统一致性的讨论,看了一些文章我有一些不解.大多数对分布式系统一致性的划分是将其分为三类:强一致性,顺序一致性以及弱一致性.强一致性(Strict C ...
- 用 Python 分析上网记录,发现了很多不可思议的事
摘要:分享个 Python 神工具. 长时间使用浏览器会积累大量浏览器历史记录,这些是很隐私的数据,里面甚至可能有一些不可描述的网站或者搜索记录不想让别人知道. 不过,我们自己可能会感兴趣,天天上 ...
- 小白学Python(3)——输入和输出,显示你的名字
任何计算机程序都是为了执行一个特定的任务,有了输入,用户才能告诉计算机程序所需的信息,有了输出,程序运行后才能告诉用户任务的结果. 输入是Input,输出是Output,因此,我们把输入输出统称为In ...
- C#中appium自动化执行移动命令mobile:shell用法
官网:https://appium.readthedocs.io/en/latest/en/commands/mobile-command/#android 1.执行ADB shell命令(需要设置服 ...
- LInux系统@安装CentOS7虚拟机
安装Centos7虚拟机 1.打开VMware,点击创建新的虚拟机(至关重要) 2.选择自定义配置,点击下一步 3.选择虚拟机硬件兼容性<Workstation 12.0>,点击下一步 4 ...
- Shell总结2
1.---------->>sed命令用来取指定范围行. 2.--------------->>>>“$?“返回值的用法 (1)判断命令.脚本或函数等程序是否执行成 ...