部署你的NginX

當我們安裝完 nginx 後除了可以拿來當網站伺服器或者當成反向代理伺服器使用,當還在新手時我們也總要了解 nginx 的設定檔是如何配置部署的,本篇就來做個簡單的了解。

主要設定檔 /etc/nginx/nginx.conf
#############################################
# 這是 Nginx 服務的主要設定檔
# 檔案位置預設為 /etc/nginx/nginx.conf
#############################################

# 啟用程序的 Linux 帳戶
user  nginx;

# 啟用的執行緒數量(建議為你的 CPU 核心數 x 2)
worker_processes  2;

# Error Log 檔的位置
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    # 允許同一時間連線總數量
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    # 預設的 log 記錄格式
    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 檔的位置
    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    # 預設不會自動啟動 gzip 壓縮
    #gzip  on;

    # 載入 /etc/nginx/conf.d/ 下的所有設定檔
    # 通常都是各個虛擬主機的配置
    include /etc/nginx/conf.d/*.conf;
}

其他配置檔位置(虛擬主機) /etc/nginx/conf.d/*.conf
預設會自動產生兩個範例設定檔,其中 default.conf 為一般虛擬主機設定檔,example_ssl.conf 為 SSL 的虛擬主機設定檔。

預設主機的配置 /etc/nginx/conf.d/default.conf

server {
    # 這個虛擬主機的 Port 和名稱
    listen       80;
    server_name  localhost;

    # 預設編碼,但通常不建議開啟,讓網頁中的 meta 或 header 自行定義
    #charset koi8-r;

    # 可以額外針對這個站台修改 log 的存放位置
    #access_log  /var/log/nginx/log/host.access.log  main;

    # 根目錄的設定
    location / {
     # 實際的檔案位置
        root   /usr/share/nginx/html;
     # 預設首頁檔名
        index  index.html index.htm;
    }

    # 如果發生 404 可以指定到特定的頁面來顯示
    #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   /usr/share/nginx/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;
    #}
}
 
以上僅是簡單的設定,想知道更多嗎?可以參考官網上的設定
http://wiki.nginx.org/Configuration

留言

這個網誌中的熱門文章

c語言-關於#define用法

CMD常用網管指令

PHP 與 JavaScript 之間傳值利用 json