[Nginx]Nginx的基本配置與優化1(完整配置示例與虛擬主機配置)


----------------------------------------------------------------------------------------

完整配置示例:

【 nginx.conf 】

#運行用戶
user www-data;
#啟動進程, 通常設置成和cpu的數量相等; 用cat /proc/cpuinfo查看cpu信息, cpu cores一欄顯示內核數 worker_processes 1; #全局錯誤日志及PID文件(/usr/local/nginx/logs/error.log) error_log /var/log/nginx/error.log; #進程id存儲文件(/usr/local/nginx/logs/nginx.pid) pid /var/run/nginx.pid; #工作模式及連接數上限 events { use epoll; #epoll是多路復用IO(I/O Multiplexing)中的一種方式,但是僅用於linux2.6以上內核,可以大大提高nginx的性能 worker_connections 1024; #單個后台worker process進程的最大並發鏈接數 # multi_accept on; #在nginx獲得有新連接的通知之后,接受盡可能多的連接, 如果worker_connections設置太低的話,這樣可能會造成擁堵 } #設定http服務器,利用它的反向代理功能提供負載均衡支持 http { #設定mime類型,類型由mime.type文件定義,可以打開mime.type來看(/usr/local/nginx/conf/mime.types) #MIME(Multipurpose Internet Mail Extensions)多用途互聯網郵件擴展類型. include /etc/nginx/mime.types; #默認文件類型以二進制數據傳輸 default_type application/octet-stream; #設定日志文件(/usr/local/nginx/logs/access.log) access_log /var/log/nginx/access.log; #關閉在錯誤頁面中的nginx版本數字,這樣對於安全性是有好處的。 server_tokens off; #sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用, #必須設為 on,如果用來進行下載等應用磁盤IO重負載應用,可設置為 off,以平衡磁盤與網絡I/O處理速度,降低系統的uptime. sendfile on; #告訴nginx在一個數據包里發送所有頭文件,而不一個接一個的發送 #tcp_nopush on; #給客戶端分配keep-alive連接超時時間 #keepalive_timeout 0; keepalive_timeout 65; #告訴nginx不要緩存數據,而是一段一段的發送;當需要及時發送數據時,就應該給應用設置這個屬性,這樣發送一小塊數據信息時就不能立即得到返回值 tcp_nodelay on; #開啟gzip壓縮 gzip on; #指定的客戶端禁用gzip功能。我們設置成IE6或者更低版本以使我們的方案能夠廣泛兼容。 gzip_disable "MSIE [1-6]\.(?!.*SV1)"; #設定請求緩沖 、、 client_header_buffer_size 1k; 、、 large_client_header_buffers 4 4k; open_file_cache max=100000 inactive=20s; #打開緩存的同時也指定了緩存最大數目,以及緩存的時間; 我們可以設置一個相對高的最大時間,這樣我們可以在它們不活動超過20秒后清除掉。 open_file_cace_valid 30s; #在open_file_cache中指定檢測正確信息的間隔時間 open_file_cache_min_uses 2; #定義了open_file_cache中指令參數不活動時間期間里最小的文件數 open_file_cache_errors on; #指定了當搜索一個文件時是否緩存錯誤信息,也包括再次給配置中添加文件。我們也包括了服務器模塊,這些是在不同文件中定義的。如果你的服務器模塊不在這些位置,你就得修改這一行來指定正確的位置。 #虛擬主機的配置文件 include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; #設定負載均衡的服務器列表 upstream mysvr { #weigth參數表示權值,權值越高被分配到的幾率越大 #本機上的Squid開啟3128端口 server 192.168.8.1:3128 weight=5; server 192.168.8.2:80 weight=1; server 192.168.8.3:80 weight=6; } server { #偵聽80端口 listen 80; #定義使用www.xx.com訪問 server_name www.xx.com; #設定本虛擬主機的訪問日志 access_log logs/www.xx.com.access.log main; #默認請求 location / { root /root; #定義服務器的默認網站根目錄位置 index index.php index.html index.htm; #定義首頁索引文件的名稱 fastcgi_pass www.xx.com; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include /etc/nginx/fastcgi_params; } # 定義錯誤提示頁面 error_page 500 502 503 504 /50x.html; location = /50x.html { root /root; } #靜態文件,nginx自己處理 location ~ ^/(images|javascript|js|css|flash|media|static)/ { root /var/www/virtual/htdocs; #過期30天,靜態文件不怎么更新,過期可以設大一點,如果頻繁更新,則可以設置得小一點。 expires 30d; } #PHP 腳本請求全部轉發到 FastCGI處理. 使用FastCGI默認配置. location ~ \.php$ { root /root; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name; include fastcgi_params; } #設定查看Nginx狀態的地址 location /NginxStatus { stub_status on; access_log on; auth_basic "NginxStatus"; auth_basic_user_file conf/htpasswd; } #禁止訪問 .htxxx 文件 location ~ /\.ht { deny all; } } }

 

其他優化文章:http://www.oschina.net/translate/nginx-setup 

 

-------------------------------------------------------------------------------------------------------

 

" 手動配置基於域名的虛擬主機 "
步驟:
1. 在nginx.conf中的http段中, 一個server段就表示一個服務主機, 每添加一個虛擬主機, 增加一個server段即可;
2. 如果單獨新建目錄存放虛擬主機配置文件, 只需要在http段中最后include進來, 如: include vhost/dog.farwish.conf;
3. 這個dog.farwish.conf中只有server段的配置內容, 如:

server {
    listen       80;
    server_name  dog.farwish.com;

    location / {
        index  index.html index.htm index.php;
        root   /home/www/dog;
        if (!-e $request_filename){   
            rewrite ^(.*)$ /index.php/?s=$1 last; # TP rewrite模式
            break;
        }
 
        autoindex on;
    }   

    location ~ \.php {
        root           /home/www/dog;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }   
}
# 以上配置是在nginx1.6.2源碼包配置文件的基礎上改動.

4. 編輯hosts文件, vim /etc/hosts, 加入127.0.0.1   dog.farwish.com

 

-------------------------------------------------------------------------------------------------------

 

一鍵安裝包中的配置:

  1. 綁定域名

    vim /etc/hosts  #添加格式如:192.168.1.11 www.chenwei.ws

  2. 添加虛擬主機   

    cd到根目錄, 一鍵安裝包安裝的運行vhost.sh添加虛擬主機, 輸入域名即可

  3. 編輯配置文件

    vim /usr/local/nginx/conf/vhost/www.chenwei.ws.conf #配置如下:

 

server
  {
    listen 80;
    #listen [::]:80;
    server_name www.chenwei.ws;           #服務器名稱
    index index.html index.htm index.php default.html default.ht m default.php;
    root /home/wwwroot/default/www.chenwei.ws;  #主機訪問對應目錄

    include other.conf;
    #error_page 404 /404.html;
  }

  #跨域調用字體時添加
  location ~* \.(eot|ttf|woff)$ {
    add_header Access-Control-Allow-Origin *;
  }

 

  4. PHP配置open_basedir(將用戶訪問文件的活動范圍限制在指定的區域, open_basedir即為訪問目錄):

    vim /usr/local/php/etc/php.ini  

   使用命令G 將光標移至行尾, 配置php訪問的主機根目錄; 如果正確, 則不需要更改:

    [HOST=www.chenwei.ws]

    [PATH=/home/wwwroot/default/www.chenwei.ws]

    open_basedir=/home/wwwroot/default/www.chenwei.ws/:/tmp/

 

Link: http://www.cnblogs.com/farwish/p/4042525.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM