Nginx&&PHP-FPM配置及最佳化指南(下)

來源:互聯網
上載者:User
Nginx&&PHP-FPM配置及最佳化指南(上)

本文介紹在Centos5.8/6.2&&RedHat(RHEL) 5.8/6.2下LEMP/LNMP環境下的Nginx&&PHP-FPM的WEB伺服器配置及最佳化指南。

截至目前,各軟體版本為

  • Nginx 1.2.2
  • PHP && PHP-FPM5.4.4

如果您還沒有搭建LEMP環境,可以參照我之前寫過一篇文章 LEMP(或LNMP)高效能的WEB伺服器在CentOS6.2/5.8下的Yum搭建流程。在"LEMP搭建指南"中我只給出了Nginx&&PHP-FPM最基本的配置說明。

在本文中將更深入的介紹Nginx&&PHP-FPM的WEB伺服器配置。

Nginx 設定檔也可以參考:http://wiki.nginx.org/NginxChs

Nginx&&PHP-FPM配置及最佳化指南(上)

Nginx的設定檔

Nginx的設定檔放在/etc/nginx路徑之下,運行ls -l /etc/nginx 輸出

total 36drwxr-xr-x. 2 root root 4096 Jul 11 19:52 conf.d-rw-r--r--. 1 root root  964 Jul  3 19:53 fastcgi_params-rw-r--r--. 1 root root 2837 Jul  3 19:53 koi-utf-rw-r--r--. 1 root root 2223 Jul  3 19:53 koi-win-rw-r--r--. 1 root root 3463 Jul  3 19:53 mime.types-rw-r--r--. 1 root root  643 Jul  3 19:50 nginx.conf-rw-r--r--. 1 root root  596 Jul  3 19:53 scgi_params-rw-r--r--. 1 root root  623 Jul  3 19:53 uwsgi_params-rw-r--r--. 1 root root 3610 Jul  3 19:53 win-utf

主設定檔nginx.conf詳細說明

#運行使用者user  nginx;#進程數目,通常設定成和cpu的數量相等worker_processes  1;#全域錯誤記錄檔error_log  /var/log/nginx/error.log warn;#PID檔案pid        /var/run/nginx.pid;#工作模式及串連數上限events {    #單個worker_process進程的最大並發連結數    worker_connections  1024;}#設定http伺服器,利用它的反向 Proxy功能還可以提供負載平衡支援http {    #設定mime類型,類型由mime.type檔案定義    include       /etc/nginx/mime.types;    #制定預設MIME類型為二進位位元組流    default_type  application/octet-stream;    #日誌格式,參考URL    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  /var/log/nginx/access.log  main;    #開啟調用Linux的sendfile(),提供檔案傳輸效率    #sendfile一般設為 on,如果用來進行下載等應用磁碟IO重負載應用,可設定為 off,以平衡磁碟與網路I/O處理速度,降低系統的uptime    sendfile        on;    #是否允許使用socket的TCP_NOPUSH或TCP_CORK選項    #tcp_nopush     on;    #指定用戶端串連保持活動的逾時時間,在這個時間之後,伺服器會關掉串連    keepalive_timeout  65;    #設定開啟gzip壓縮,參考URL    #gzip  on;    #虛擬機器主機設定檔引入    include /etc/nginx/conf.d/*.conf;}

主設定檔nginx.conf參數最佳化要點

Nginx&&PHP-FPM配置及最佳化指南(上)

1. worker_processes及 worker_connections配置

預設配置中worker_processes及 worker_connections的數目有點小,只能應付1000次/秒以內的請求。

#預設配置worker_processes  1;worker_connections  1024;

通常情況下,worker_processes設定為cpu數目,worker_connections保持1024即可。你可以使用cat /proc/cpuinfo |grep processor來查看CPU數量

2. 隱藏Ngnix版本資訊

server_tokens off;

3. 拒絕web訪問系統隱藏檔案

location ~ /\. {    access_log off;    log_not_found off;     deny all;}

4. 限制最大檔案上傳大小

client_max_body_size 20m;client_body_buffer_size 128k;

5. Nginx靜態檔案快取控制

瀏覽器緩衝非常有利於節省頻寬,在Nginx中非常容易配置

location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {    access_log        off;    log_not_found     off;    expires           360d;}


6. Ngnix轉寄PHP請求至PHP-FPM

# Pass PHP scripts to PHP-FPMlocation ~* \.php$ {    try_files       $uri /index.php;    fastcgi_index   index.php;    fastcgi_pass    127.0.0.1:9000;    #fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;    include         fastcgi_params;    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;}
7. 開啟GZIP壓縮

gzip on;gzip_min_length  1k;gzip_buffers     4 16k;gzip_http_version 1.0;gzip_comp_level 2;gzip_types       text/plain application/x-javascript text/cssapplication/xml;gzip_vary on;

********************************************
* 作者:葉文濤
* 本文連結: Nginx&&PHP-FPM配置及最佳化指南(上)
******************轉載請註明來源 ***************


  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.