共计 16695 个字符,预计需要花费 42 分钟才能阅读完成。
研究了 2 天的 nginx 缓存 出现各种报错
我把配置文件修改过后还是报错
location ~ /purge(/.*)
{
allow 127.0.0.1;
allow 192.168.0.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
nginx: [emerg] unknown directive “proxy_cache_purge” in /home/data/websrv/nginx/conf/nginx.conf:75
nginx: [emerg] unknown directive “proxy_cache_purge
报错提示
发现模块没有安装,有的重新安装模块
安装 ngx_cache_purge-1.3.tar
wget http://labs.frickle.com/files/ngx_cache_purge-2.0.tar.gz
tar -zxvf ngx_cache_purge-2.0.tar.gz
cd /home/data/install/lnmp/nginx-1.0.15/
查看版本
nginx -V
[root@ceshi nginx-1.0.15]# /home/data/websrv/nginx/sbin/nginx -V
nginx version: nginx/1.0.15
configure arguments: –prefix=/home/data/websrv/nginx –with-http_stub_status_module
啥配置都没 所以只能重新编译
./configure –user=nginx –group=nginx –add-module=/home/data/install/ngx_cache_purge-2.0 \
–prefix=/home/data/websrv/nginx –with-http_stub_status_module –with-http_ssl_module
加模块
onfiguration summary
+ using system PCRE library
+ using system OpenSSL library
+ md5: using OpenSSL library
+ sha1: using OpenSSL library
+ using system zlib library
nginx path prefix: “/home/data/websrv/nginx”
nginx binary file: “/home/data/websrv/nginx/sbin/nginx”
nginx configuration prefix: “/home/data/websrv/nginx/conf”
nginx configuration file: “/home/data/websrv/nginx/conf/nginx.conf”
nginx pid file: “/home/data/websrv/nginx/logs/nginx.pid”
nginx error log file: “/home/data/websrv/nginx/logs/error.log”
nginx http access log file: “/home/data/websrv/nginx/logs/access.log”
nginx http client request body temporary files: “client_body_temp”
nginx http proxy temporary files: “proxy_temp”
nginx http fastcgi temporary files: “fastcgi_temp”
nginx http uwsgi temporary files: “uwsgi_temp”
nginx http scgi temporary files: “scgi_temp”
注意这里只要 make 而不要 make install
make,不要 make install 会覆盖
-o objs/src/http/ngx_http_postpone_filter_module.o \
src/http/ngx_http_postpone_filter_module.c
gcc -c -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/modules/ngx_http_ssi_filter_module.o \
src/http/modules/ngx_http_ssi_filter_module.c
gcc -c -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/modules/ngx_http_charset_filter_module.o \
src/http/modules/ngx_http_charset_filter_module.c
gcc -c -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/modules/ngx_http_userid_filter_module.o \
src/http/modules/ngx_http_userid_filter_module.c
gcc -c -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/modules/ngx_http_autoindex_module.o \
src/http/modules/ngx_http_autoindex_module.c
gcc -c -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/modules/ngx_http_auth_basic_module.o \
src/http/modules/ngx_http_auth_basic_module.c
make[1]: Leaving directory `/home/data/install/lnmp/nginx-1.0.15′
make -f objs/Makefile manpage
make[1]: Entering directory `/home/data/install/lnmp/nginx-1.0.15′
sed -e “s|%%PREFIX%%|/home/data/websrv/nginx|” \
-e “s|%%PID_PATH%%|/home/data/websrv/nginx/logs/nginx.pid|” \
-e “s|%%CONF_PATH%%|/home/data/websrv/nginx/conf/nginx.conf|” \
-e “s|%%ERROR_LOG_PATH%%|/home/data/websrv/nginx/logs/error.log|” \
< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/home/data/install/lnmp/nginx-1.0.15′
替换 nginx 二进制文件
[root@ceshi nginx-1.0.15]# killall nginx
[root@ceshi nginx-1.0.15]# cp ./objs/nginx /home/data/websrv/nginx/sbin/
cp:是否覆盖 ”/home/data/websrv/nginx/sbin/nginx”?y
service nginx start
这次就没报错了
location ~ /purge(/.*)
{
allow 127.0.0.1;
allow 192.168.0.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
access_log off;
}
Starting nginx daemon: nginx
started.
正常启动 nginx 了
当是发现 nginx 页面是 500
500 Internal Server Error
[root@ceshi nginx-1.0.15]# ulimit -n
655360
[root@ceshi nginx-1.0.15]# ulimit -n 65535
[root@ceshi nginx-1.0.15]# cat /proc/sys/fs/file-max
65535
解决,也可以直接改配置文件
vi /etc/security/limits.conf
在文件末加上:
* soft nofile 65535
* hard nofile 65535
127.0.0.1 – – [06/Nov/2013:01:52:05 +0800] “GET /static_gmd/images/shadow_bg.jpg HTTP/1.0” 500 0 “-” “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36”
服务器端会判断上次的 config 时间是否比 If-Modified-Since 晚。如果自上次 request 之后又更新了 config,那么服务器就会返回完整的内容;如果期间没有更新 config,那么服务器就没必要返回完整的内容,只需要向客户端发送一个 304 Not Modified 状态码就可以了。
更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2013-11/93021p2.htm
推荐阅读:
Nginx 实现反向代理和负载均衡的配置及优化 http://www.linuxidc.com/Linux/2013-11/92909.htm
Nginx 做负载均衡报:nginx: [emerg] could not build the types_hash http://www.linuxidc.com/Linux/2013-10/92063.htm
Nginx 负载均衡模块 ngx_http_upstream_module 详述 http://www.linuxidc.com/Linux/2013-10/91907.htm
Nginx+Firebug 让浏览器告诉你负载均衡将请求分到了哪台服务器 http://www.linuxidc.com/Linux/2013-10/91824.htm
Ubuntu 安装 Nginx php5-fpm MySQL(LNMP 环境搭建) http://www.linuxidc.com/Linux/2012-10/72458.htm
Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里
这样就可以了
[root@ceshi websrv]# cd cache/
[root@ceshi cache]# ll
总用量 4
drwx——. 3 coovanftp coovanftp 4096 11 月 20 01:17 b
[root@ceshi cache]# cd b/
[root@ceshi b]# ll
总用量 4
drwx——. 2 coovanftp coovanftp 4096 11 月 20 01:18 9b
[root@ceshi b]# cd 9b/
[root@ceshi 9b]# ll
总用量 4
-rw——-. 1 coovanftp coovanftp 800 11 月 20 01:18 beffd3cafb1fbf7c3b53aa486005b9bb
但是又发现一个问题
[root@ceshi nginx]# netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’
TIME_WAIT 2735
ESTABLISHED 6
cp 0 0 192.168.0.21:80 192.168.0.21:27544 TIME
tcp 0 0 192.168.0.21:80 192.168.0.21:26458 TIME
tcp 0 0 192.168.0.21:80 192.168.0.21:27665 TIME
tcp 0 0 192.168.0.21:80 192.168.0.21:26601 TIME
tcp 0 0 192.168.0.21:80 192.168.0.21:27333 TIME
tcp 0 0 192.168.0.21:80 192.168.0.21:26498 TIME
tcp 0 0 192.168.0.21:80 192.168.0.21:25969 TIME
tcp 0 0 192.168.0.21:80 192.168.0.21:26061 TIME
tcp 0 0 192.168.0.21:80 192.168.0.21:26777 TIME
tcp 0 0 192.168.0.21:80 192.168.0.21:26960 TIME
tcp 0 0 192.168.0.21:80 192.168.0.21:27391 TIME
tcp 0 0 192.168.0.21:80 192.168.0.21:26568 TIME
tcp 0 0 192.168.0.21:80 192.168.0.21:26525
发现系统存在大量 TIME_WAIT 状态的连接,通过调整内核参数解决,
vi /etc/sysctl.conf
编辑文件,加入以下内容:
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
然后执行 /sbin/sysctl -p 让参数生效。
[root@ceshi nginx]# netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’
TIME_WAIT 1
ESTABLISHED 4
[root@ceshi nginx-1.0.15]# netstat -anplt
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address Stat e PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LIST EN 20565/php-fpm.conf)
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LIST EN 16446/mysqld
tcp 0 0 0.0.0.0:80 0.0.0.0:* LIST EN 25492/nginx.conf
tcp 0 0 0.0.0.0:22 0.0.0.0:* LIST EN 1768/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LIST EN 1150/master
tcp 0 52 192.168.0.21:22 192.168.0.224:51384 ESTA BLISHED 22344/1
tcp 0 0 192.168.0.21:22 192.168.0.224:52369 ESTA BLISHED 22398/sshd: root@no
tcp 0 0 192.168.0.21:22 192.168.0.224:61398 ESTA BLISHED 20420/0
tcp 0 0 192.168.0.21:22 192.168.0.224:62132 ESTA BLISHED 20773/sshd: root@no
tcp 0 0 192.168.0.21:80 192.168.0.224:53048 TIME _WAIT –
tcp 0 0 :::22 :::* LIST EN 1768/sshd
tcp 0 0 ::1:25 :::* LIST EN 1150/master
drwx——. 2 coovanftp root 4096 11 月 20 09:56 proxy_cache_path
drwx——. 2 coovanftp root 4096 11 月 20 09:56 proxy_temp
[root@ceshi nginx]# netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’
TIME_WAIT 1
ESTABLISHED 4
[root@ceshi cache]# ll
总用量 8
drwx——. 3 coovanftp coovanftp 4096 11 月 20 11:55 a
drwx——. 3 coovanftp coovanftp 4096 11 月 20 11:54 b
最后附上配置
user coovanftp coovanftp;
worker_processes 8;
error_log logs/error.log crit;
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http {
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”‘;
server_tokens off;
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
gzip on;
gzip_types text/plain application/x-javascript text/css application/xml;
server_names_hash_bucket_size 2048;
client_header_buffer_size 2048k;
large_client_header_buffers 4 2048k;
client_max_body_size 500m;
client_body_buffer_size 4048k;
fastcgi_buffer_size 2048k;
fastcgi_buffers 6 2048k;
fastcgi_busy_buffers_size 2048k;
fastcgi_temp_file_write_size 2048k;
fastcgi_intercept_errors on;
proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_buffer_size 2048k;
proxy_buffers 4 2048k;
proxy_busy_buffers_size 2048k;
#proxy_cache_valid 200 304 302 3s;
proxy_temp_file_write_size 4096k;
proxy_temp_path /home/data/websrv/nginx/temp_dir;
proxy_cache_path /home/data/websrv/nginx/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=10g;
# upstream 指令用于设置一组可以在 proxy_pass 和 fastcgi_pass 指令中使用的代理服务器, 默认的负载均衡方式为轮询.upstream 模块中的 server 指令用于指定后端服务器的名称和参数, 服务器的名称可以是一个域名,一个 ip 地址, 端口号或者 UNIX Socket.
#而在 server{..} 虚拟主机内, 可以通过 proxy_pass 和 fastcgi_pass 指令设置进行反向代理的 upstream 服务器集群
# proxy_set_header 指令用于在向反向代理的后端 WEB 服务器发起请求时添加指定的 header 头信息
#当后端 WEB 服务器上有多个基于域名的虚拟主机时, 要通过添加 header 头信息 Host, 用于指定请求的域名, 这样后端服务器才能识别该反向代理访问请求是由那一个虚拟主机来处理
#使用反向代理之后, 后端 web 服务器就不能直接 $_SERVER[‘REMOTE_ADDR’]变量来获取用户的真实 ip 了, 通过 $_SERVER[‘REMOTE_ADDR’]获得的将是负载均衡器的 ip. 这时, 就要通过 Nginx 反向代理时添加 Header 头信息 X -Forwarded-For, 让后端 web 服务器能够通过 $_SERVER[‘HTTP_X_FORWARDED_FOR’]获取到用户的真实 ip
#nginx 的 proxy_cache 相关指令集
# 1.proxy_cache: 该指令用于设置那个缓存区将被使用
# 2.proxy_cache_path: 该指令用于设置缓存文件的存放路径
#示例:proxy_cache_path /web/server1 levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g
# 该指令只能在 http 标签内配置,levels 指定该缓存空间有两层 hash 目录, 第一层为 1 个字母, 第二层为 2 个字母;
# keys_zone 参数用来为这个缓存区起名,500m 指内存缓存空间大小为 500MB;inactive 的 1d 指如果缓存数据在 1 天内没有被访问, 将被删除;max_size 的 30g 是指硬盘缓存空间为 30GB
#3.proxy_cache_methods: 该指令用于设置缓存那些 http 方法, 默认缓存 get 和 head, 不缓存 post
#4.proxy_cache_min_uses: 该指令用于设置缓存的最小使用次数, 默认为 1
#5.proxy_cache_valid: 该指令用于对不同返回状态码的 url 设置不同的缓存时间
upstream iis{
server 192.168.0.21;
}
server {
listen 80;
server_name 192.168.0.21;
location / {
index index.php index.html;
proxy_pass http://iis;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
proxy_cache_valid 200 5m;
proxy_cache_valid 304 5m;
proxy_cache_valid 301 302 1h;
proxy_cache_valid any 5m;
proxy_cache_key http://$host$uri$is_args$args;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
研究了 2 天的 nginx 缓存 出现各种报错
我把配置文件修改过后还是报错
location ~ /purge(/.*)
{
allow 127.0.0.1;
allow 192.168.0.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
nginx: [emerg] unknown directive “proxy_cache_purge” in /home/data/websrv/nginx/conf/nginx.conf:75
nginx: [emerg] unknown directive “proxy_cache_purge
报错提示
发现模块没有安装,有的重新安装模块
安装 ngx_cache_purge-1.3.tar
wget http://labs.frickle.com/files/ngx_cache_purge-2.0.tar.gz
tar -zxvf ngx_cache_purge-2.0.tar.gz
cd /home/data/install/lnmp/nginx-1.0.15/
查看版本
nginx -V
[root@ceshi nginx-1.0.15]# /home/data/websrv/nginx/sbin/nginx -V
nginx version: nginx/1.0.15
configure arguments: –prefix=/home/data/websrv/nginx –with-http_stub_status_module
啥配置都没 所以只能重新编译
./configure –user=nginx –group=nginx –add-module=/home/data/install/ngx_cache_purge-2.0 \
–prefix=/home/data/websrv/nginx –with-http_stub_status_module –with-http_ssl_module
加模块
onfiguration summary
+ using system PCRE library
+ using system OpenSSL library
+ md5: using OpenSSL library
+ sha1: using OpenSSL library
+ using system zlib library
nginx path prefix: “/home/data/websrv/nginx”
nginx binary file: “/home/data/websrv/nginx/sbin/nginx”
nginx configuration prefix: “/home/data/websrv/nginx/conf”
nginx configuration file: “/home/data/websrv/nginx/conf/nginx.conf”
nginx pid file: “/home/data/websrv/nginx/logs/nginx.pid”
nginx error log file: “/home/data/websrv/nginx/logs/error.log”
nginx http access log file: “/home/data/websrv/nginx/logs/access.log”
nginx http client request body temporary files: “client_body_temp”
nginx http proxy temporary files: “proxy_temp”
nginx http fastcgi temporary files: “fastcgi_temp”
nginx http uwsgi temporary files: “uwsgi_temp”
nginx http scgi temporary files: “scgi_temp”
注意这里只要 make 而不要 make install
make,不要 make install 会覆盖
-o objs/src/http/ngx_http_postpone_filter_module.o \
src/http/ngx_http_postpone_filter_module.c
gcc -c -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/modules/ngx_http_ssi_filter_module.o \
src/http/modules/ngx_http_ssi_filter_module.c
gcc -c -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/modules/ngx_http_charset_filter_module.o \
src/http/modules/ngx_http_charset_filter_module.c
gcc -c -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/modules/ngx_http_userid_filter_module.o \
src/http/modules/ngx_http_userid_filter_module.c
gcc -c -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/modules/ngx_http_autoindex_module.o \
src/http/modules/ngx_http_autoindex_module.c
gcc -c -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/modules/ngx_http_auth_basic_module.o \
src/http/modules/ngx_http_auth_basic_module.c
make[1]: Leaving directory `/home/data/install/lnmp/nginx-1.0.15′
make -f objs/Makefile manpage
make[1]: Entering directory `/home/data/install/lnmp/nginx-1.0.15′
sed -e “s|%%PREFIX%%|/home/data/websrv/nginx|” \
-e “s|%%PID_PATH%%|/home/data/websrv/nginx/logs/nginx.pid|” \
-e “s|%%CONF_PATH%%|/home/data/websrv/nginx/conf/nginx.conf|” \
-e “s|%%ERROR_LOG_PATH%%|/home/data/websrv/nginx/logs/error.log|” \
< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/home/data/install/lnmp/nginx-1.0.15′
替换 nginx 二进制文件
[root@ceshi nginx-1.0.15]# killall nginx
[root@ceshi nginx-1.0.15]# cp ./objs/nginx /home/data/websrv/nginx/sbin/
cp:是否覆盖 ”/home/data/websrv/nginx/sbin/nginx”?y
service nginx start
这次就没报错了
location ~ /purge(/.*)
{
allow 127.0.0.1;
allow 192.168.0.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
access_log off;
}
Starting nginx daemon: nginx
started.
正常启动 nginx 了
当是发现 nginx 页面是 500
500 Internal Server Error
[root@ceshi nginx-1.0.15]# ulimit -n
655360
[root@ceshi nginx-1.0.15]# ulimit -n 65535
[root@ceshi nginx-1.0.15]# cat /proc/sys/fs/file-max
65535
解决,也可以直接改配置文件
vi /etc/security/limits.conf
在文件末加上:
* soft nofile 65535
* hard nofile 65535
127.0.0.1 – – [06/Nov/2013:01:52:05 +0800] “GET /static_gmd/images/shadow_bg.jpg HTTP/1.0” 500 0 “-” “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36”
服务器端会判断上次的 config 时间是否比 If-Modified-Since 晚。如果自上次 request 之后又更新了 config,那么服务器就会返回完整的内容;如果期间没有更新 config,那么服务器就没必要返回完整的内容,只需要向客户端发送一个 304 Not Modified 状态码就可以了。
更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2013-11/93021p2.htm
推荐阅读:
Nginx 实现反向代理和负载均衡的配置及优化 http://www.linuxidc.com/Linux/2013-11/92909.htm
Nginx 做负载均衡报:nginx: [emerg] could not build the types_hash http://www.linuxidc.com/Linux/2013-10/92063.htm
Nginx 负载均衡模块 ngx_http_upstream_module 详述 http://www.linuxidc.com/Linux/2013-10/91907.htm
Nginx+Firebug 让浏览器告诉你负载均衡将请求分到了哪台服务器 http://www.linuxidc.com/Linux/2013-10/91824.htm
Ubuntu 安装 Nginx php5-fpm MySQL(LNMP 环境搭建) http://www.linuxidc.com/Linux/2012-10/72458.htm
Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里