生活随笔
收集整理的這篇文章主要介紹了
nginx 缓存配置及报错解决
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
研究了2天的nginx緩存 出現各種報錯
我把配置文件修改過后還是報錯
| 1 2 3 4 5 6 7 | 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; ????????????????} |
| 1 | nginx: [emerg] unknown directive?"proxy_cache_purge"?in?/home/data/websrv/nginx/conf/nginx.conf:75 |
| 1 | nginx: [emerg] unknown directive "proxy_cache_purge |
報錯提示
發現模塊沒有安裝,有的重新安裝模塊
| 1 2 3 4 5 6 | 安裝 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 |
| 1 2 3 | [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 |
啥配置都沒 所以只能重新編譯
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | ./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
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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二進制文件
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [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
| 1 | 500 Internal Server Error |
| 1 2 3 4 5 | [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 |
解決,也可以直接改配置文件
| 1 2 3 4 | vi?/etc/security/limits.conf 在文件末加上: ?*??????????????? soft? nofile???????? 65535 ?*??????????????? hard? nofile???????? 65535 |
| 1 | 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" |
| 1 | 服務器端會判斷上次的 config 時間是否比 If-Modified-Since 晚。如果自上次 request 之后又更新了 config,那么服務器就會返回完整的內容;如果期間沒有更新 config,那么服務器就沒必要返回完整的內容,只需要向客戶端發送一個 304 Not Modified 狀態碼就可以了。 |
這樣就可以了
| 1 2 3 4 5 6 7 8 9 10 11 12 | [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 |
但是又發現一個問題
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | [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 |
最后附上配置
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | 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; ????} } } 本文轉自 cs312779641 51CTO博客,原文鏈接:http://blog.51cto.com/chenhao6/1329106 |
總結
以上是生活随笔為你收集整理的nginx 缓存配置及报错解决的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。