動態焦點:nginx高并發優化之模板

2022-12-21 18:18:03 來源:51CTO博客


(相關資料圖)

下面的Nginx.conf實現nginx在前端做反向代理服務器的完整配置文件的例子,處理js、png等靜態文件,jsp/php等動態請求轉發到其它服務器tomcat/apacheuser  www www;worker_processes  auto;worker_cpu_affinity auto;error_log  logs/error.log;worker_rlimit_nofile 102400;pid        logs/nginx.pid;events {    use epoll;    worker_connections  102400;}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""                      ""$upstream_cache_status"";    access_log  logs/access.log  main;    server_tokens off;    sendfile        on;    tcp_nopush     on;    keepalive_timeout  65;    #Compression Settings    gzip on;    gzip_comp_level 6;    gzip_http_version 1.1;    gzip_proxied any;    gzip_min_length 1k;    gzip_buffers 16 8k;    gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;    gzip_vary on;    #end gzip    # http_proxy Settings    client_max_body_size   10m;    client_body_buffer_size   128k;    proxy_connect_timeout   75;    proxy_send_timeout   75;    proxy_read_timeout   75;    proxy_buffer_size   4k;    proxy_buffers   4 32k;    proxy_busy_buffers_size   64k;    proxy_temp_file_write_size  64k;    proxy_buffering on;    proxy_temp_path /usr/local/nginx1.10/proxy_temp;    proxy_cache_path /usr/local/nginx1.10/proxy_cache levels=1:2 keys_zone=my-cache:100m max_size=1000m inactive=600m max_size=2g;    #load balance Settings    upstream backend {        hash $remote_addr consistent;        server 192.168.31.141:80 weight=1 max_fails=2 fail_timeout=10s;        server 192.168.31.250:80 weight=1 max_fails=2 fail_timeout=10s;    }    #virtual host Settings    server {        listen       80;        server_name  localhost;        charset utf-8;        location  ~/purge(/.*) {           allow 127.0.0.1;           allow 192.168.31.0/24;           deny all;           proxy_cache_purge my-cache $host$1$is_args$args;        }        location / {            index  index.php index.html index.htm;            proxy_pass        http://backend;            proxy_redirect off;            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_ignore_headers Set-Cookie;            proxy_hide_header Set-Cookie;            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;        }        location ~ .*\.(gif|jpg|png|html|htm|css|js|ico|swf|pdf)(.*) {           proxy_pass  http://backend;           proxy_redirect off;           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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;           proxy_cache my-cache;           add_header Nginx-Cache $upstream_cache_status;           proxy_cache_valid 200 304 301 302 8h;           proxy_cache_valid 404 1m;           proxy_cache_valid any 1d;           proxy_cache_key $host$uri$is_args$args;           expires 30d;           access_log off;        }        location /nginx_status {            stub_status on;            access_log off;            allow 192.168.31.0/24;            deny all;        }    }}$upstream_cache_status這個變量來顯示緩存的狀態,我們可以在配置中添加一個http頭來顯示這一狀態,$upstream_cache_status包含以下幾種狀態: ·MISS 未命中,請求被傳送到后端 ·HIT 緩存命中 ·EXPIRED 緩存已經過期請求被傳送到后端 ·UPDATING 正在更新緩存,將使用舊的應答 ·STALE 后端將得到過期的應答expires : 在響應頭里設置Expires:或Cache-Control:max-age,返回給客戶端的瀏覽器緩存失效時間。

標簽: 失效時間 反向代理 服務器的

上一篇:環球快報:從源碼層面深度剖析Spring循環依賴
下一篇:天天實時:循環語句