nginx配置文件详解不生效,什么问题

随笔- 104&
评论- 222&
&&&&&&&&&&&
windows 下配置 Nginx 常见问题
因为最近的项目需要用到负载均衡,不用考虑,当然用大名鼎鼎的Nginx啦。至于Nginx的介绍,这里就不多说了,直接进入主题如何在下配置。
我的系统是win7旗舰版的,到官网下载最新版本&解压到英文目录下(我刚开始是放到中文目录下的,启动时会有问题,下面常见错误里会讲到)。
一、& Nginx配置
找到 conf 目录里的 nginx.conf 文件,配置Nginx
#指定nginx进程数
worker_processes
#全局错误日志及PID文件
#error_log
logs/error.
#error_log
logs/error.
#error_log
logs/error.
logs/nginx.
# 连接数上限
worker_connections
#设定http服务器,利用它的反向代理功能提供负载均衡支持
#设定mime类型,类型由mime.type文件定义
default_type
application/octet-
#设定日志格式
#log_format
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#使用哪种格式的日志
#access_log
logs/access.
#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
#连接超时时间
#keepalive_timeout
keepalive_timeout
#开启gzip压缩
#设定负载均衡的服务器列表 支持多组的负载均衡,可以配置多个upstream& 来服务于不同的Server.
#nginx 的 upstream 支持 几 种方式的分配
#1)、轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
#2)、weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 跟上面样,指定了权重。
#3)、ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。&
#4)、fair &&&&&
#5)、url_hash #Urlhash
upstream mysvr {
#weigth参数表示权值,权值越高被分配到的几率越大
#1.down 表示单前的server暂时不参与负载
#2.weight 默认为1.weight越大,负载的权重就越大。
#3.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
#server 192.168.1.116
#server 192.168.1.116
server 192.168.1.121
server 192.168.1.122
#配置代理服务器的地址,即Nginx安装的服务器地址、监听端口、默认地址
#1.侦听80端口
#对于server_name,如果需要将多个域名的请求进行反向代理,可以配置多个server_name来满足要求
#charset koi8-r;
#access_log
logs/host.access.
location / {
# 默认主页目录在nginx安装目录的html子目录。
index.html index.
proxy_pass http:// #跟载均衡服务器的upstream对应
#error_page
# redirect server error pages to the static page /50x.html
## 定义错误提示页面
#error_page
500 502 503 504
#location = /50x.html {
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#location ~ \.php$ {
proxy_pass
http://127.0.0.1;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#location ~ \.php$ {
fastcgi_pass
127.0.0.1:9000;
fastcgi_index
fastcgi_param
SCRIPT_FILENAME
/scripts$fastcgi_script_
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#location ~ /\.ht {
# another virtual host using mix of IP-, name-, and port-based configuration
somename:8080;
server_name
location / {
index.html index.
# HTTPS server
ssl_certificate
ssl_certificate_key
ssl_session_cache
shared:SSL:1m;
ssl_session_timeout
ssl_ciphers
HIGH:!aNULL:!MD5;
ssl_prefer_server_
location / {
index.html index.
二、& 启动Nginx
cmd 进入Nginx解压目录 执行 start nginx启动Nginx服务
启动后如何检查是否启动成功呢? 输入命令 tasklist /fi "imagename eq nginx.exe"&& 看到以下信息说明启动成功了
一切就绪,访问一下server 里配置的&server_name 是不是被重定向到&upstream配置的服务器上了,是不是很简单!
三、常见错误
如果启动失败 可以看下logs目录下 error.log 文件里的错误信息。
我在第一次安装的时遇到两个错误,也是最容易碰到的问题,在这里列出来方便大家碰到相同的问题时快速解决。
1.& 端口占用问题
我的配置文件里服务侦听的是 80 端口,由于机器上部署了IIS,80端口被默认站点占用,把站点关闭就可以了,这个问题在错误日志里记录是这样的。
10:44:12 [emerg] : bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
碰到类似的错误,请确认端口是否被占用或被防火墙屏蔽
2.Nginx所在目录有中文
错误日志大致输出一下内容
11:55:55 [emerg] : CreateFile() "E:\软件\nginx-1.7.8/conf/nginx.conf" failed (1113: No mapping for the Unicode character exists in the target multi-byte code page)
3. 启用缓存时报错
17:26:50 [emerg] : shared zone "cache_one" has no equal addresses: 02CF0000 vs 02A20000
17:26:50 [alert] : worker process 17068 exited with code 1
我一直没有找到解决的方法,有人说重启服务,或者缓存设置大一点就可以了,我试了一下没有用的,&原文是这样讲的,只能认为windwos下无解了。
: The cache and other modules which require shared memory support do
: not work in Windows Vista and later due to address space layout
: randomization being enabled in these Windows versions.
阅读(...) 评论()系统重启后ngix reload不生效原因分析
重启后ngix reload不生效原因分析
这是一种比较少见,困扰我很久的问题,虽然这个问题很简单,但是找到根本原因还是费了不少时间,现在把分析过程分享如下。
前提:需要对系统启动过程、Nginx进程启动过程及进程跟踪有一定的理解。
一,Nginx reload过程分析:
经过查看官网文档及结合Nginx分析,大致得出reload过程进行了如下操作。
1,检查配置是否正确
相当于nginx -t
2,打开日志文件
相当于nginx -s reopen
由于日志文件比较多,需要打开多个文件
3,重新监听套接字
相当于nginx
这个步骤会初始化很多东西,重点关注哈希表
4,关闭旧worker进程
相当于nginx -s quit
二,nginx进程分析
1,首先了解nginx的两种进程
master进程,root用户打开,接收信号,管理worker进程
worker进程,nginx用户打开,工作进程,负责处理http请求
2,starce跟踪主进程号,期间执行nginx -s reload,发现卡在检查日志文件这块
主进程跟踪,因为reload过程是系统发送HUP信号给nginx主进程
#starce -p 2298
open(&/data/wwwlogs/access.xxx.xxx.xxx.log&, O_WRONLY|O_CREAT|O_APPEND, 0644) = -1 EMFILE (Too many open files)
write(808, & 09:50:22 [emerg] 2298&..., 124) = 124
3,根据提示,查找进程的系统限制文件
master进程限制
# cat /proc/2398/limits
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size
unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 015 processes
Max open files
Max locked memory
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 015 signals
Max msgqueue size 200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
worker进程限制
# cat /proc/2300/limits
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size
unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 015 processes
Max open files 600 files
Max locked memory
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 015 signals
Max msgqueue size 200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
补充错误日志:
10:48:05 [notice] 47386#0: signal process started
10:48:05 [emerg] 2298#0: open() &/data/wwwlogs/access_xxx.xxx.xxx.log& failed (24: Too many open files)
三,解决方案
1,修改限制
一般从以下3方面调优:
第一:nginx.conf参数规划与设置
worker_rlimit_nofile :限制单个工作进程打开的最大文件数:
线上配置没有问题
worker_rlimit_nofile 409600;
第二:系统级别的检查与设置
就是 /etc/security/limits.conf的配置与修改,请参考Linux系统资源限制汇总
线上配置没有问题
* soft nofile 655350
* hard nofile 655350
第三:内核级别的检查与设置:
fs.file-max值的大小设置:
线上配置比较大
fs.file-max = 6553600
注意:file-max的默认值大概是系统内存的10%(系统内存以kb计算)
2,验证生效
结果发现以上配置前期都有配置,但是重启服务器发现主进程的限制并没有修改过来,但是登陆服务器后无论在终端ulimit -n 查看还是关闭nginx主进程后重启nginx都生效了,由此推理出
问题可能出在linux系统启动过程中,也就是说nginx主进程启动时,上面的限制配置没有生效,后来查阅资料发现系统启动后执行login时才会使limits.conf配置生效,所以需要调整顺序。
根据实际情况,系统启动过程如下:
1、读取/etc/inittab来读取默认级别 假设:读取到的默认级别是 3
2、执行初始化系统脚本 /etc/rc.d/rc.sysinit 来初始化脚本
3、然后执行 /etc/rc.d/rc 脚本
4、执行/etc/rc.d/rc.local脚本,此脚本是启动过程中最后启动的一个脚本。
最后会执行 /bin/login 登录用户。至此系统启动过程完成,login时才会执行/etc/profile,~/.bash_profile和~/.bashrc等,此时的ulimit -n查到的值不是nginx进程启动时的值。
默认用户登陆时会使limits.conf配置文件生效,这个比nginx进程启动晚,要在这之前使配置生效,需要补充配置如下:
cat /etc/rc.local
ulimit -HSn 655350 (注意在nginx启动前执行)
/usr/local/nginx/sbin/nginx
四,补充优化
主要是相关参数调大了一些。
1,内核优化
net.ipv4.tcp_max_tw_buckets 修改大一些,减少内核负担,iptable本身对内核性能有影响
# ss -an |awk '{print $1}'|sort |uniq -c |sort -rn
15415 ESTAB
12979 TIME-WAIT
1961 FIN-WAIT-2
501 FIN-WAIT-1
234 LAST-ACK
32 SYN-RECV
1 SYN-SENT
1 CLOSE-WAIT
线上修改配置如下:
net.ipv4.tcp_max_tw_buckets = 18000
2,nginx优化
主要是哈希表,其他配置已经优化,哈希表有如下几种
server_names_hash可以加
map_hash可以加
types_hash够用
request header 不考虑
variables_hash 够用
线上修改配置如下:
server_names_hash_max_size 512000;
server_names_hash_bucket_size 64; (默认)
map_hash_max_size 204800;
map_hash_bucket_size 64; (默认)
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'}

我要回帖

更多关于 nginx 配置没有生效 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信