nginx安裝配置|nginx負載均衡|nginx反向代理|gzip壓縮|expires緩存

Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。由俄羅斯的程序設計師Igor Sysoev所開發,供俄國大型的入口網站及搜索引擎Rambler(俄文:Рамблер)使用。其特色是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁服務器中表現較好javascript

模塊依賴性php

gzip模塊須要 zlib 庫
rewrite模塊須要 pcre 庫
ssl 功能須要openssl庫css

安裝PCRE庫html

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz 
tar -zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure
make && make install

安裝zlib庫java

wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make && make install

安裝ssl

自由選擇是否須要編譯mysql

wget http://www.openssl.org/source/openssl-1.0.1g.tar.gz
tar -zxf openssl-1.0.1g.tar.gz
cd openssl-1.0.1g
./config
make && make install

安裝nginxnginx

cd nginx
./configure --prefix=/usr/local/nginx
make & make install

啓動nginxweb

cd/usr/local/nginx
./sbin/nginx

配置nginx開機自動啓動sql

打開/etc/rc.d/rc.local文件添加nginx文件路徑

/usr/local/nginx/sbin/nginx

重啓nginxapache

./sbin/nginx -s reload

查看訪問日誌

tail -10 logs/haowan.access.log

nginx設置定時分割日誌文件

#!/bin/bash
LOGPATH=/usr/local/nginx/logs/haowan.access.log  // nginx日誌原路徑
BASEPATH=/date/$(date -d yesterday +%Y%m)  //切割後存放目錄
mkdir -p $BASEPATH  //按月建立文件夾
bak=$BASEPATH/$(date -d yesterday +%d%H%M).haowan.access.log  //計算路徑與日誌命名
mv $LOGPATH $bak
touch $LOGPATH
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
添加cut_logs.sh執行權限

chmod +x /usr/local/nginx/date/cut_logs.sh
設置cut_logs.sh啓動時間,執行命令crontab -e進入編輯狀態:

01 00 * * * /usr/local/nginx/date/runlog.sh
能夠用如下方式來執行此腳本:

/etc/init.d/nginx start
 /etc/init.d/nginx stop
 /etc/init.d/nginx reload
 /etc/init.d/nginx restart

nginx rewrite重寫

nginx經過ngx_http_rewrite_module模塊支持url重寫、支持if條件判斷,但不支持else。該模塊須要PCRE支持,應在編譯nginx時指定PCRE源碼目錄

判斷瀏覽器類型

if ($http_user_agent ~* Mozilla){
        rewrite ^.*$ /ie.html;
        break;
      }
    # 404錯誤頁面
    if (!-e $document_root$fastcgi_script_name){
        rewrite ^.*$ /404.html;
        break;
    }
    if ($http_user_agent ~* Mozilla){
        set $isit 1;
    }
    if ($fastcgi_script_name  ie.html){
        set $isie 0;
    }
    if($isie 1){
       rewrite ^.*$ /ie.html
    }

php與nginx整合

注意:編譯的php要有以下功能:鏈接mysql,gd,ttf, 以fpm(fascgi)方式運行。

./configure \
   --prefix=/usr/local/fastphp\
   --with-mysql=/usr/local/mysql\
   --with-gd\
   --enable-gd-native-ttf\
   --enable-gd-jis-conv\
   --enable-fpm
  make && make install
複製php配置文件

cp php.ini-development /usr/local/fastphp/lib/php.ini
複製 php-fpm配置文件

cp etc/php-fpm.conf.default  etc/php-fpm.conf
啓動php-fpm

./sbin/php-fpm

查看進程

ps aux | grep php

設置nginx.conf文件

php與nginx整合

location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
    include        fastcgi_params;
}

nginx gzip壓縮提高網站速度

網頁壓縮是一項由 WEB 服務器和瀏覽器之間共同遵照的協議,也就是說 WEB 服務器和瀏覽器都必須支持該技術,所幸的是如今流行的瀏覽器都是支持的,包括 IE、FireFox、Opera 等;服務器有 Apache 和 IIS 等。雙方的協商過程以下:首先瀏覽器請求某個 URL 地址,並在請求的頭 (head) 中設置屬性 accept-encoding 值爲 gzip, deflate,代表瀏覽器支持 gzip 和 deflate 這兩種壓縮方式(事實上 deflate 也是使用 gzip 壓縮協議。

gzip經常使用參數:

gzip on|off  #是否開啓gzip
gzip_buffers 32 4k|16 8k  #緩衝
gzip_comp_level[1-9]  #推薦6壓縮級別
gzip_disable  #正則匹配UA 什麼樣的Uri不進行gzip
gzip_min_length 200  #開始壓縮的最小長度(再小就不要壓縮了)
gzip_http_version1.0|1.1  #開始壓縮的http協議版本
gzip_proxied  #設置請求代理服務器該如何緩存內容
gzip_types text/plain application/xml  #對哪些類型的文件用壓縮如:text/xml/html/css
gzip_vary on|off  #是否傳輸gzip壓縮標誌
nginx gzip壓縮設置

gzip on;
gzip_buffers 32 4k;
gzip_comp_level 6;
gzip_min_length 2000;
gzip_types text/css text/xml application/x-javascript;
nginx的expires緩存設置提升網站性能

Nginx設置expires設定頁面緩存時間 不緩存或一直使用緩存 配置expires expires起到控制頁面緩存的做用,合理的配置expires能夠減小不少服務器的請求

location ~* \.(jpg|jpeg|gif|png){
  root html;
  expires 1d;  # 緩存一天
 }

反向代理實現 nginx+apache動靜分離

nginx反向代理的指令不須要新增額外的模塊,默認自帶proxy_pass指令,只須要修改配置文件就能夠實現反向代理。支持兩個用法 proxy與upstream,分別用來作反向代理和負載均衡

#配置apache虛擬主機 
    <VirtualHost *:8080>
        ServerAdmin webmaster@dummy-host2.example.com
        DocumentRoot "/usr/local/nginx/html"
        ServerName "192.168.80.22"
        ErrorLog "logs/dummy-host2.example.com-error_log"
        CustomLog "logs/dummy-host2.example.com-access_log" common
    </VirtualHost>
#設置權限
 <Directory />
   Options FollowSymLinks
   AllowOverride None
   Order deny,allow
   Allow from all
 </Directory>
設置nginx配置文件 .php文件讓apache來解析

 location ~ \.php$ {
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_pass   http://192.168.80.22:8080;
      
 }

nginx負載均衡配置

*nginx負載均衡主要是對七層網絡通訊模型中的第七層應用層上的http、https進行支持。同時nginx更新版本也在逐步對Websocket、SPDY等協議做出支持。
nginx是以反向代理的方式進行負載均衡的。反向代理(Reverse Proxy)方式是指以代理服務器來接受Internet上的鏈接請求,而後將請求轉發給內部網絡上的服務器,並將從服務器上獲得的結果返回給Internet上請求鏈接的客戶端,此時代理服務器對外就表現爲一個服務器。*

配置兩臺虛擬機

server{
     listen 81;
     server_name 127.0.0.1;
     root html;
     access_log logs/81-access.log main;
   }
   server{
       listen 82;
       server_name 127.0.0.1;
       root html;
       access_log logs/82-access.log main;
   }

把多臺服務器用upstream指定綁定在一塊兒並起個組名

upstream imgserver{
  server 127.0.0.1:81 weight=1 max_fails=2 fail_timeout=3;
  server 127.0.0.1:82 weight=1 max_fails=2 fail_timeout=3;
 }

而後用proxy_pass 指向imgserver

location ~* \.(jpg|jpeg|gif|png){
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_pass http://imgserver;
 }
相關文章
相關標籤/搜索