源碼安裝Nginx及配置文件詳解

 

一、安裝Nginx

二、Nginx編譯選項

三、Nginx進程管理命令

四、Nginx配置文件解析

 

 

一、安裝Nginx

1、提前安裝所需軟件依賴包

yum install -y gcc pcre pcre-devel openssl openssl-devel gd gd-devel perl perl-ExtUtils-Embed

2、下載Nginx源碼安裝包

wget http://nginx.org/download/nginx-1.4.0.tar.gz

3、解壓編譯(禁用模塊使用參數--without,編譯第三方模塊使用參數--add-module=/path/module)

tar -xzf nginx-1.4.0.tar.gz -C /usr/src/
cd /usr/src/nginx-1.4.0/
./configure --prefix=/usr/local/nginx \
--with-ipv6 \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-http_perl_module \
--with-mail \
--with-mail_ssl_module

4、安裝

make && make install

 

安裝完成後,程序主目錄位於/usr/local/nginx/,目錄下的內容分別爲:  
conf,主配置文件目錄  
html,網站根目錄  
logs,日誌文件目錄  
sbin,主程序目錄

 

 

二、Nginx編譯選項

 

1、默認自動編譯項                           禁用選項
Core:Nginx核心功能,						--without-http
Access:基於IP的訪問控制					--without-http_access_module
Auth Basic:HTTP用戶認證模塊				--without-http_auth_basic_module
Auto Index:自動目錄索引					--without-http_autoindex_module
Browser:描述用戶代理						--without-http_charset_module
Charset:重新編碼網頁						--without-http_charset_module
Empty GIF:內存中存放一個圖片				--without-http_empty_gif_module
FastCGI:FastCGI支持						--without-http_fastcgi_module
Geo:支持IP變量設置							--without-http_geo_module
Gzip:Gzip壓縮								--without-http_gzip_module
Limit Requests:限制客戶端連接頻率			--without-http_limit_req_module
Limit Conn:揮發的併發連接					--without-http_limit_conn_module
Map:設置變量								--without-http_map_module
Memcached:Memcache支持						--without-http_memcached_module
Referer:基於Referer頭部信息過濾			--without-http_referer_module
Rewrite:使用正則表達式重寫請求				--without-http_rewrite_module
SCGI:支持SCGI協議							--without-http_scgi_module
Upstream:負載均衡							--without-http_upstream_ip_hash_module
Headers:設置http響應的頭部信息
Index:首頁
Log:自定義日誌

 

2、內置模塊中的附加模塊,需要在編譯時手動開啓	開啓選項
Embedded Perl:支持Perl							--with-http_perl_module
FLV:支持Flash視頻								--with-http_flv_module
GeoIP:通過IP變量實現負載均衡					--with-http_geoip_module
Google Perftools:支持谷歌的性能優化工具		--with-google_perftools_module
Gzip Precompression:壓縮靜態文件				--with-http_gzip_static_module
Image Filter:轉換圖形的過濾器					--with-http_image_filter_module
MP4:支持MP4									--with-http_mp4_module
Real IP:使用Nginx作爲後端服務器				--with-http_realip_module
Secure Link:使用密匙保護頁面					--with-http_secure_link_module
SSL:支持HTTPS/SSL 								--with-http_ssl_module
Stub Status:查看服務器狀態						--with-http_stub_status_module
WebDAV:支持WebDAV								--with-http_dav_module
------------------------------------------
Core:郵件代理功能								--with-mail
Core:郵件代理功能								--without-mail_pop3_module
Core:郵件代理功能								--without-mail_imap_module
Core:郵件代理功能								--without-mail_smtp_module
------------------------------------------
SSL:支持SSL/TLS加密郵件協議					--with-mail_ssl_module

 

三、Nginx進程管理命令

 

/usr/local/nginx/sbin/nginx     #啓動主程序
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf     #指定配置文件啓動主程序
/usr/local/nginx/sbin/nginx -s stop     #關閉主程序
/usr/local/nginx/sbin/nginx -s reload     #重新加載設置

Nginx會將進程號保存在/usr/local/nginx/logs/nginx.pid文件中,可以使用kill指令發送信號給該進程號平滑重啓Nginx  
 

kill -HUP `cat /usr/local/nginx/logs/nginx.pid`


 

四、Nginx配置文件解析

 

Nginx默認配置文件爲/usr/local/nginx/conf/nginx.conf,其內容如下

 

#user  nobody;	#設置用戶與組
worker_processes  1;	#啓動子進程數,可以通過ps aux | grep nginx查看

#錯誤日誌文件及日誌級別notice,info
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;	#進程號保存文件


events {
    worker_connections  1024;	#每個進程可以處理的連接數,受系統文件句柄限制,ulimit
}


http {
    include       mime.types;	#mime.types爲文件類型定義文件
    default_type  application/octet-stream;	#默認文件類型

    #log_format自定義入職格式,名稱爲main
    #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  logs/access.log  main;		#創建訪問日誌,採用main定義的格式

    sendfile        on;		#是否調用sendfile() 進行數據複製,sendfile複製數據是在內核級別完成的,會比一般的read、write更高效
    #tcp_nopush     on;

    #keepalive_timeout  0;		#保持連接的超時時間
    keepalive_timeout  65;

    #gzip  on;		#是否採用壓縮功能,將頁面壓縮後傳輸更節省流量

    #使用server定義虛擬主機
    server {
        listen       80;		#服務器監聽的端口
        server_name  localhost;		#訪問域名

        #charset koi8-r;		#編碼格式,如果網頁編碼與此設置不同,則將被自動轉碼

        #access_log  logs/host.access.log  main;		#設置虛擬主機的訪問日誌路徑及格式

        #對URL進行匹配
        location / {
            root   html;		#root表示網頁根路徑,使用的是相對路徑,html在nginx安裝路徑下
            index  index.html index.htm;		#定義index首頁文件,先找index.html,沒有則找index.htm
        }


        #設置錯誤代碼對應的錯誤頁面
        #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   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #訪問以php結尾的頁面,則自動將該請求轉至127.0.0.1:80,通過proxy_pass可以實現代理功能
        #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
        #
        #拒絕所有人訪問.ht頁面
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;		#監聽TLS使用的
    #    server_name  localhost;

    #    ssl                  on;   #開啓ssl功能
    #    ssl_certificate      cert.pem;		#指定證書文件,相對路徑,文件需放在niginx.conf同目錄下
    #    ssl_certificate_key  cert.key;		#指定私鑰文件,相對路徑,文件需放在niginx.conf同目錄下

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章