顯示具有 Nginx 標籤的文章。 顯示所有文章
顯示具有 Nginx 標籤的文章。 顯示所有文章

2013年7月3日 星期三

Nginx 設定檔相關說明

Setting

主要設定檔位置: /etc/nginx/nginx.conf
引入設定檔路徑: include /etc/nginx/conf.d/*.conf
伺服器設定檔: /etc/nginx/conf.d/default.conf

nginx.conf 設定檔內容

user              nginx;
worker_processes  8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 1048576;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /run/nginx.pid;

events {
    use   epoll;
    worker_connections  1048576;
}

http {
    include       /etc/nginx/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"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip on;
    gzip_comp_level 6;
    gzip_vary on;
    gzip_min_length 1000;
    gzip_proxied any;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_buffers 16 8k;

    include /etc/nginx/conf.d/*.conf;

    upstream node_server {
        server 192.168.1.1:3000 weight=2;
        server 192.168.1.1:3001 weight=1;
    }
}

設定說明:

>>> user        nginx;
nginx 為使用者名稱
>>> worker_processes  8;
需要開啟的 worker 數量,盡量以核心數為設定標準
>>> worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
綁定每個 worker process 給 CPU 核心
>>> worker_rlimit_nofile 1048576;
要和系统單一 process 的 open file 數量一致
Note
The Linux kernel has a hard upper limit of 1024*1024
>>> error_log  /var/log/nginx/error.log;
錯誤紀錄檔 參考
>>> pid        /run/nginx.pid;
Pid文件位置

event 區塊

>>> use epoll;
epoll 是 I/O Multiplexing 中的一種方式, 用於linux kernel 2.6以上,可提高nginx的效能
>>> worker_connections  1048576;
每個 worker 的大連接數,理論上每台 nginx 伺服器的最大連接數為 worker_processes * worker_connections

http 區塊

>>> include       /etc/nginx/mime.types;
設定MIME, 由mime.type 檔案來定義
>>> default_type  application/octet-stream;
設定預設的MIME类型
>>> 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;
設定 access 紀錄檔
>>> sendfile        on;
開啟 sendfile(), 指定Nginx 使用 kernel 的 sendfile 函數來提高 web 傳輸文件的效率,一般設為 on,如果用來進行下載等應用 磁碟IO 重負載的應用,可設為 off,以平衡磁碟與網路 I/O 處理速度,降低系統的負載。 P.S.如果圖片顯示不正常把這個改成off
>>> tcp_nopush      on;
開啟 TcpNopush(TCP_CORK), 採用 Nagle 算法較小的 packet 存入緩衝區直到蓄足一個 frame 後一起發送,這樣可以最小化所發送的 message 的數量來提高應用程序的效率,用以縮減網路傳輸過程中的封包數量,並減輕網絡擁塞問題。 可參考 TcpNopush
>>> keepalive_timeout 65 5;
第一個參數指定 Client 端連接保持活動的限制時間,在這個時間之後,Server 會關掉連接,第二個參數是可選的,它指定了消息頭保持活動的有效時間,即響應中的 timeout=time,它可以告訴某些瀏覽器關閉連接,因此服務器就不必關閉連接了,如果沒有這個參數,Nginx不會發送Keep-Alive頭。
>>> gzip  on;
開啟gzip壓縮 參考資料: GzipModule
>>> include /etc/nginx/conf.d/*.conf;
讀取來自於 /etc/nginx/conf.d 資料夾的 config 檔案, 預設的 Server 設定在 conf.d/default.conf
>>> upstream node_server {
        server 192.168.1.146:3000 weight=2;
        server 192.168.1.146:3001 weight=1;
    }
開啟負載平衡, weight 是用來設定 server 分配權重

Nginx 安裝

Nginx

輕量級的web server, reverse proxy server 與apahce & IIS 角色相同
epoll 非同步non-blocking I/O
下面為Linux下的安裝方式

Install

Fedora 17
yum install nginx
CentOS 6
vim /etc/yum.repos.d/nginx.repo
nginx.repo >
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

2012年10月3日 星期三

Nginx 反向代理與Rewite

以下為筆記 :p
延續上次的設定

反向代理 

利用proxy_pass來設定欲導向的服務
修改/etc/nginx/conf.d 底下的設定檔
我在此新增一個localnginx.conf的設定檔
server {
    listen       80;
    server_name  localnginx1;
    
    charset utf-8;
    
    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}

listen跟server_name請看上篇
重點在於 location / 的部份
 / 代表根目錄
也就是在網址輸入 http://localnginx1/ 的時候
要處理的動作
這裡我使用 proxy_pass 把 client 端發出的 Request
轉發到 3000 port 的 service

Url Rewrite 
rewrite 可以接兩個參數
第一個參數設下條件
第二個參數則設定目標
server {
    listen       80;
    server_name  localnginx2;
    
    charset utf-8;

    location / {
        rewrite  ^(.*) http://127.0.0.1:3000;
    }
    
    location /google {
        rewrite ^ http://www.google.com.tw;
    }
}

按照上面的方式設定
輸入 http://localnginx2/ 時
會導向 http://127.0.0.1:3000; 並改變網址

輸入 http://localnginx2/google 時
會導向 google 台灣首頁


Nginx Virtual Host 設定

最近開始碰Nginx 紀錄一下設定的部份 環境為FC17 kernel: 3.5.3-1.fc17.i686.PAE 安裝好Nginx後 先設定為開機啟動 在Terminal上輸入
systemctl enable nginx.service
成功之後 輸入啟動指令
systemctl start nginx.service

完成後輸入在瀏覽器網址列上輸入localhost應該就能看到Nginx的歡迎頁面了


接下來為了測試方便 我們使用修改 hostname 的方式來模擬 sub domain 的部份 先編輯 /etc/hosts

如上圖所示, 在127.0.0.1那行加上 localnginx ~ localnginx2 三個name來測試 設定完畢就開始進入Nginx的設定了 Nginx的conf檔置於 /etc/nginx/conf.d/底下 只要在此目錄下的.conf檔案都會在Nginx啟動時載入 新增一個設定檔
vim /etc/conf.d/localnginx.conf

server {
    listen       80;
    server_name  localnginx;

    charset utf-8;

    location / {
        root   /home/sean/nginx/;
        index  index.html index.htm;
    }
}
此設定檔設定偵聽 80 port 並在遇到server name為localnginx時 使用此設定檔 粗體的部份就是設定此server的初始目錄在哪 存檔完畢後輸入重新啟動nginx
systemctl reload nginx.service
在對應的目錄底下放入index.html後 如此一來在瀏覽器上就能看到結果囉