整理nginxconf配置詳細

來源:互聯網
上載者:User
##定義nginx啟動並執行使用者各使用者組
user nginx nginx;
##nginx進程數,建議設定與cpu核心數一致
worker_processes 1;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
##全域錯誤記錄檔定義類型[ debug | info | notice | warn | error | crit ]
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
##一個nginx進程開啟的最多檔案描述符數目,理論值應該是最多開啟檔案數(系統的值ulimit -n)與nginx進程數相除,但是nginx分配請求並不均勻,所以建議與ulimit -n的值保持一致。
worker_rlimit_nofile 65535;
##進程檔案
#pid logs/nginx.pid;
##工作模式與串連數上限
events {
  ##參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本核心中的高效能網路I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
  use epoll;
  ##單個進程的最大串連數
  worker_connections 65535;
}
##設定http伺服器
http {
  ##引入外置設定檔
  include /etc/nginx/conf.d/*.conf;
  ##副檔名與檔案類型映射表
  include mime.types;
  ##預設檔案類型
  default_type application/octet-stream;
  ##預設編碼
  #charset utf-8;
  ##伺服器名字的hash表大小
  #server_name_hash_bucket_size 128;
  ##上傳檔案大小限制 建議開啟
  client_header_buffer_size 32K;
  ##設定請求緩衝 建議開啟
  large_client_header_buffers 4 64K;
  ##最大緩衝
  client_max_body_size 20M;
client_header_timeout 20;
  ##日誌格式設定
  #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  # '$status $body_bytes_sent "$http_referer" '
  # '"$http_user_agent" "$http_x_forwarded_for"';
  ##訪問日誌
  #access_log logs/access.log main;
  ##開啟高效檔案傳輸模式sendfile指令指定nginx是否調用sendfile函數來輸出檔案,對於普通應用設為 on,如果用來進行下載等應用磁碟IO重負載應用,可設定為off,以平衡磁碟與網路I/O處理速度,降低系統的負載。注意:如    果圖片顯示不正常把這個改成off。
  sendfile on;
  ##開啟目錄列表訪問,合適下載伺服器,預設關閉
  #autoindex on;
  ##防止網路阻塞 建議開啟
  tcp_nopush on;
  ##防止網路阻塞 建議開啟
  tcp_nodelay on;
  ##長連結逾時時間,單位是秒,為0,無逾時  
  keepalive_timeout 65;
  ##gzip模組設定
  ##開啟gzip壓縮輸出 建議開啟
  gzip on;
  ##最小壓縮檔大小 建議開啟
  gzip_min_length 1k;
  ##壓縮緩衝區 建議開啟
  gzip_buffers 4 16k;
  ##壓縮版本(預設1.1,前端如果squid2.5請使用1.0) 建議開啟
  gzip_http_version 1.0;
  ##壓縮等級
  gzip_comp_level 2; 建議開啟
  ##壓縮類型,預設就已經包含了textxml,預設不用寫,寫上去也沒有問題,會有一個warn 建議開啟
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
  ##開啟串連限制ip串連數使用
  #limit_zone crawler $binary_remote_addr 10m;
##反向 Proxy緩衝Proxy Cache配置
proxy_temp_path /opt/cache/nginx/temp;
proxy_cache_path /opt/cache/nginx/cache levels=1:2 keys_z inactive=1d max_size=2g;
proxy_cache_path /opt/cache/nginx/proxy_cache_image levels=1:2 keys_z inactive=1d max_size=10g;
proxy_connect_timeout 30;
proxy_read_timeout 60;
proxy_send_timeout 20;
proxy_buffer_size 96k;
proxy_buffers 8 256k;
proxy_busy_buffers_size 512k;
proxy_temp_file_write_size 512k;
#proxy_cache_path配置
#keys_z 表示這個zone名稱為infcache,分配的記憶體大小為600MB
#/opt/cache/nginx/cache 表示cache這個zone的檔案要存放的目錄
#levels=1:2 表示緩衝目錄的第一級目錄是1個字元,第二級目錄是2個字元,即/data/ngx_cache/cache1/a/1b這種形式
#inactive=1d 表示這個zone中的快取檔案如果在1天內都沒有被訪問,那麼檔案會被cache manager進程刪除掉
#max_size=10g 表示這個zone的硬碟容量為10GB

  ##FastCGI相關參數是為了改善網站的效能:減少資源佔用,提高訪問速度。
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;

  ##負載平衡,weight權重,權值越高被分配到的幾率越大
  upstream myserver{
    server 192.168.1.10:8080 weight=3;
    server 192.168.1.11:8080 weight=4;
    server 192.168.1.12:8080 weight=1;
  }
  ##虛擬機器主機配置
  server {
    ##監聽連接埠
    listen 80;
    ##網域名稱可以有多個,用空格隔開
    server_name localhost;
    #charset koi8-r;
    ##定義本虛擬機器主機的訪問日誌
    #access_log logs/host.access.log main;
    location / {
      root html;
      index index.html index.htm;
    }
    ##圖片緩衝時間設定
    location ~.*.(gif|jpg|jpeg|png|bmp|swf)${
      expires 10d;
    }  
    ##js和CSS緩衝時間設定
    location ~.*.(js|css)?${
      expires 1h;
    }
    #error_page 404 /404.html;
    # redirect server error pages to the static page /50x.html
    #error_page 500 502 503 504 /50x.html;
    location = /50x.html {
      root 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$ {
      # root html;
      # fastcgi_pass 127.0.0.1:9000;
      # fastcgi_index index.php;
     # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
      # include fastcgi_params;
    #}
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
      # deny all;
    #}
    ##對 "/" 啟用反向 Proxy
    location / {
      ##或者使用
      #proxy_pass http://myserver;
      proxy_pass http://127.0.0.1:88;
      proxy_redirect off;
      proxy_set_header X-Real-IP $remote_addr;#後端的Web伺服器可以通過X-Forwarded-For擷取使用者真實IP
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      #以下是一些反向 Proxy的配置,可選。
      proxy_set_header Host $host;
      client_max_body_size 10m;       #允許用戶端請求的最大單檔案位元組數
      client_body_buffer_size 128k;      #緩衝區代理緩衝使用者端請求的最大位元組數,
      proxy_connect_timeout 90;       #nginx跟後端伺服器連線逾時時間(代理連線逾時)
      proxy_send_timeout 90;         #後端伺服器資料回傳時間(代理髮送逾時)
      proxy_read_timeout 90;         #串連成功後,後端伺服器回應時間(代理接收逾時)
      proxy_buffer_size 4k;           #設定Proxy 伺服器(nginx)儲存使用者頭資訊的緩衝區大小
      proxy_buffers 4 32k;           #proxy_buffers緩衝區,網頁平均在32k以下的設定
      proxy_busy_buffers_size 64k;       #高負荷下緩衝大小(proxy_buffers*2)
      proxy_temp_file_write_size 64k;      #設定快取檔案夾大小,大於這個值,將從upstream伺服器傳
    }
    ##設定查看Nginx狀態的地址
    location /NginxStatus {
      stub_status on;
      access_log on;
      auth_basic "NginxStatus";
      auth_basic_user_file confpasswd;
      #htpasswd檔案的內容可以用apache提供的htpasswd工具來產生。
    }
    ##本地動靜分離反向 Proxy配置
    #所有jsp的頁面均交由tomcat或resin處理
    location ~ .(jsp|jspx|do)?$ {
      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://127.0.0.1:8080;
    }
    ##所有靜態檔案由nginx直接讀取不經過tomcat或resin
    location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
      { expires 15d; }
    location ~ .*.(js|css)?$
      { expires 1h; }
  }
}

以上就介紹了整理nginxconf配置詳細,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.