在CentOS7上編譯安裝nginx

安裝依賴庫:

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

下載源碼包:

地址:https://nginx.org/download,選擇需要的版本,如:
wget https://nginx.org/download/nginx-1.14.2.tar.gz

解壓:

tar -zxvf nginx-1.14.2.tar.gz
cd nginx-1.14.2

配置編譯選項:

./configure --prefix=/usr/local/nginx --with-http_ssl_module

如果只需要架設(shè)一個普通的支持SSL網(wǎng)站,上面的選項也就足夠了,讀者可又跳過本節(jié)。

其它內(nèi)置選項:

--with-stream // 1.9以后版本支持TCP反向代理(區(qū)別于HTTP反向代理)
--with-http_auth_request_module // 1.12以后版本支持代理認(rèn)證

添加nginx-rtmp-module模塊:

下載:
git clone https://github.com/arut/nginx-rtmp-module.git
增加選項:
--add-module=../nginx-rtmp-module // 支持rtmp推拉流,指定模塊位置

注意:外部模塊要放在nginx的同級目錄,請留意--add-module這里都是以相對目錄載入的,下同。

添加echo-nginx-module模塊:

下載:
git clone https://github.com/agentzh/echo-nginx-module.git
增加選項:
--add-module=../echo-nginx-module // 支持echo函數(shù)

對Lua的支持:

需要先安裝LuaJIT,并且添加ngx_devel_kit,lua-nginx-module模塊:
LuaJIT下載:
官網(wǎng):http://luajit.org/
wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar xzvf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4

sudo make install PREFIX=/usr/local/luajit

注意環(huán)境變量!
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0

添加ngx_devel_kit模塊:

下載:
git clone https://github.com/simpl/ngx_devel_kit.git
增加選項:
--add-module=../ngx_devel_kit // 支持ngx工具開發(fā)

添加lua-nginx-module模塊:

下載:
git clone https://github.com/openresty/lua-nginx-module.git
增加選項:
--add-module=../lua-nginx-module // 支持lua

確定好需要的選項,統(tǒng)一配置,如:
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream --with-http_auth_request_module
--add-module=../nginx-rtmp-module
--add-module=../echo-nginx-module
--add-module=../ngx_devel_kit
--add-module=../lua-nginx-module
以上選項除了支持HTTPS站點(diǎn),還支持HTTP反向代理、TCP反向代理、RTMP直播、LUA。

編譯、安裝:

make
sudo make install

測試:

/usr/local/nginx/sbin/nginx -t

輸出以下信息,表示安裝成功:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

若輸出如下錯誤,表示/usr/local/nginx/logs目錄不存在:
nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)
2016/09/13 19:08:56 [emerg] 6996#0: open() "/usr/local/nginx/logs/access.log" failed (2: No such file or directory)

解決辦法:
sudo mkdir /usr/local/nginx/logs

若輸出錯誤,這是由于找不到libluajit-5.1.so.2:
../sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

解決辦法,使用locate libluajit發(fā)現(xiàn)安裝在/usr/local/luajit/lib目錄下:
vim /etc/ld.so.conf,加入:
/usr/local/luajit/lib
然后,sudo ldconfig

啟動:

sudo /usr/local/nginx/sbin/nginx

其它操作:
sudo /usr/local/nginx/sbin/nginx -s reload // 重新裝載配置
sudo /usr/local/nginx/sbin/nginx -s stop // 安全退出

常用配置:

連接數(shù)配置:

編輯 /usr/local/nginx/conf/nginx.conf,修改:

#worker_processes  1;
worker_processes  auto;
worker_rlimit_nofile 65535;
 
events {
#    worker_connections 1024;
     use epoll;
     worker_connections 65535;
}
......
HTTP反向代理 + 限速配置:
......
http {
    limit_conn_zone $binary_remote_addr zone=addr:10m;

    upstream proxy82 {
        server 127.0.0.1:81;
    }

    server {
        listen 82;
        location / {
            #limit_conn addr 100;
            limit_rate_after 100k;
            limit_rate 50k;
            proxy_max_temp_file_size 0;
            proxy_pass http://proxy82;
            proxy_set_header Host $host;
            proxy_set_header X-real-ip $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
        }
    }
}
......

說明:
limit_conn_zone $binary_remote_addr zone=addr:10m;
表示創(chuàng)建一個連接內(nèi)存區(qū)域,名稱為addr,大小為10M,內(nèi)容為客戶端IP。
limit_conn addr 100;
表示限制每個IP的連接數(shù)為100個。
limit_rate_after 100k;
表示在傳輸100K之后發(fā)起限速。
limit_rate 50k;
表示限制每條連接的傳輸速度在100K。
proxy_max_temp_file_size 0;
nginx做為代理時,默認(rèn)是打開響應(yīng)緩沖的,它會把從源獲取的響應(yīng)先寫入內(nèi)存中,當(dāng)內(nèi)存寫滿時會寫入臨時文件,這里設(shè)置為0表示關(guān)閉寫臨時文件。

TCP反向代理配置:
......
stream {
    upstream proxy3306 {
        #hash $remote_addr consistent;
        server 127.0.0.1:3306 weight=1 max_fails=3 fail_timeout=30s;
    }

    server {
        listen 3307;
        #proxy_pass 127.0.0.1:3306;
        proxy_pass proxy3306;
        proxy_connect_timeout 3s;
        proxy_timeout 60s;
    }
}
......

說明:
proxy_connect_timeout 3s;
表示配置連接后端超時時間為3S。
proxy_timeout 60s;
表示配置與后端連接空閑時間為60S,之后自動斷開,可用于異常連接清理。

緩存代理服配置:
......
http {
    proxy_cache_path /dev/shm/nginx/cache levels=1:2 keys_zone=shm:10m inactive=1h max_size=2g use_temp_path=off;

    upstream proxy83 {
        server 127.0.0.1:80;
    }

    server {
        listen       83;
        location / {
            proxy_pass http://proxy83;
            proxy_set_header Host $host;
            proxy_set_header X-real-ip $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_cache_key $host$uri$is_args$args;
            proxy_cache shm;
            proxy_cache_valid 200 302 10m;
            proxy_cache_valid 404 1m;
            expires 2h;
            proxy_cache_lock on;
            add_header X-Via $server_addr;
            #add_header X-Cache $upstream_cache_status;
            add_header X-Cache '$upstream_cache_status from $host';
            }
    }
}
......

說明:
proxy_cache_path /dev/shm/nginx/cache levels=1:2 keys_zone=shm:10m inactive=1h max_size=2g use_temp_path=off;
表示設(shè)置緩存的目錄(必須先創(chuàng)建)和選項,這里將目錄設(shè)置在內(nèi)存盤上:/dev/shm/nginx/cache。
proxy_cache_lock on;
表示開啟回源鎖定,防止多個請求同一時間請求同一文件時造成并發(fā)回源。

寫在最后:

本文有點(diǎn)囫圇吞棗,涉及的內(nèi)容比較多,而且很多細(xì)節(jié)都沒有描述清楚,抱歉!說明下,本文是我根據(jù)以前的日記整理的,一些細(xì)節(jié)在腦子里已經(jīng)不太清楚,而且我覺得后續(xù)有必要自己重新上機(jī)操作驗證后,再補(bǔ)充這些方面的細(xì)節(jié),歡迎關(guān)注。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容