1. 程式人生 > >編譯安裝 nginx-1.13.12

編譯安裝 nginx-1.13.12

func page proc add geo 常用 cpu geoip 全局

參考:http://www.linuxe.cn/post-168.html

1、在安裝Nginx之前需要確保系統裏已經安裝好相關環境:pcre庫(提供正則表達式和Rewrite模塊的支持)、zlib庫(提供Gzip壓縮)、openssl(提供ssl支持),如果沒有安裝的話,用Yum來安裝這些依賴環境即可,不需要額外編譯:

yum  install  pcre  pcre-devel  openssl  openssl-devel  zlib  zlib-devel  -y

2、為Nginx創建好用戶和組,後續編譯會用上

groupadd nginx
useradd -s /sbin/nologin -g nginx nginx

3、配置、編譯

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_stub_status_module
make && make install

4、

4、編譯安裝完成後會在指定的安裝目錄創建以下目錄:

prefix/sbin :  Nginx的啟動腳本,也可以用該腳本做一系列檢查工作
prefix
/conf : 配置文件存放的目錄 prefix/logs : 日誌文件目錄 prefix/html : 默認的網頁文件存放目錄
/usr/local/nginx/sbin/nginx -t #檢查配置文件是否有錯 /usr/local/nginx/sbin/nginx -v #查看Nginx版本 /usr/local/nginx/sbin/nginx -V #查看編譯Nginx時的選項

3、編譯安裝Nginx,常用選項:

--prefix=path:設置Nginx的安裝路徑,不寫的話默認是在/usr/local/nginx
--sbin-path=path:設置Nginx的可執行文件路徑,默認路徑是prefix/sbin/nginx
--conf-path=path:設置Nginx配置文件路徑,默認路徑是prefix/conf/nginx.conf
--pid-path=path:設置Nginx pid文件路徑,默認路徑是prefix/logs/nginx.pid
--error-log-path=path:設置錯誤日誌存放路徑,默認路徑是prefix/logs/error.log
--http-log-path=path:設置訪問日誌存放路徑,默認路徑是prefix/logs/access.log
--user=name:設置運行Nginx的用戶,默認用戶是nobody
--group=name:設置運行Nginx的用戶組,默認用戶組是nobody
--with-http_ssl_module enable ngx_http_ssl_module
--with-http_v2_module enable ngx_http_v2_module
--with-http_realip_module enable ngx_http_realip_module
--with-http_addition_module enable ngx_http_addition_module
--with-http_xslt_module enable ngx_http_xslt_module
--with-http_xslt_module=dynamic enable dynamic ngx_http_xslt_module
--with-http_image_filter_module enable ngx_http_image_filter_module
--with-http_image_filter_module=dynamic
enable dynamic ngx_http_image_filter_module
--with-http_geoip_module enable ngx_http_geoip_module
--with-http_geoip_module=dynamic enable dynamic ngx_http_geoip_module
--with-http_sub_module enable ngx_http_sub_module
--with-http_dav_module enable ngx_http_dav_module
--with-http_flv_module enable ngx_http_flv_module
--with-http_mp4_module enable ngx_http_mp4_module
--with-http_gunzip_module enable ngx_http_gunzip_module
--with-http_gzip_static_module enable ngx_http_gzip_static_module
--with-http_auth_request_module enable ngx_http_auth_request_module
--with-http_random_index_module enable ngx_http_random_index_module
--with-http_secure_link_module enable ngx_http_secure_link_module
--with-http_degradation_module enable ngx_http_degradation_module
--with-http_slice_module enable ngx_http_slice_module
--with-http_stub_status_module enable ngx_http_stub_status_module

5、Nginx主配置文件(/usr/local/nginx/conf/nginx.conf)詳解:
nginx的配置文件裏有很多“{}”包括起來的標簽,其中main和events都是全局性標簽,

每個主機都配置在server標簽裏

在server標簽裏的location標簽是做匹配

Location標簽的匹配規則:
~ #區分大小寫的正則匹配,
~* #不區分大小寫的正則匹配
^~ #禁止正則表達式匹配,當匹配到這裏後停止其他所有匹配
= #精確匹配

未做任何配置的配置文件:

#user  nobody;    #默認運行Nginx的用戶名
worker_processes  1;    #開啟的進程數,通常和cpu個數相等
events {
    worker_connections  1024;    #每個進程的並發數
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;      #長連接超時時間為65秒

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

編譯安裝 nginx-1.13.12