Linux:nginx安裝及負載均衡配置
主機環境信息
[root@test1280 ~]# cat /etc/redhat-release
CentOS release 6.8 (Final)
[root@test1280 ~]# uname -a
Linux test1280 2.6.32-642.el6.x86_64 #1 SMP Tue May 10 17:27:01 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
nginx 源碼安裝
1.下載nginx源碼包:(http://nginx.org/download/)
wget http://nginx.org/download/nginx-1.15.10.tar.gz
解壓:
[test1280@test1280 ~]$ tar zxf nginx-1.15.10.tar.gz
[test1280@test1280 ~]$ cd nginx-1.15.10
2.生成 makefile 文件:
[test1280@test1280 nginx-1.15.10]$ ./configure --prefix=$HOME/nginx --with-http_ssl_module
其中,–prefix指定nginx的安裝目錄,–with-http_ssl_module是爲支持HTTPS。
從configure的輸出我們可以獲取很詳細的信息,包括binary位置,日誌目錄等等:
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/home/test1280/nginx"
nginx binary file: "/home/test1280/nginx/sbin/nginx"
nginx modules path: "/home/test1280/nginx/modules"
nginx configuration prefix: "/home/test1280/nginx/conf"
nginx configuration file: "/home/test1280/nginx/conf/nginx.conf"
nginx pid file: "/home/test1280/nginx/logs/nginx.pid"
nginx error log file: "/home/test1280/nginx/logs/error.log"
nginx http access log file: "/home/test1280/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
執行 configure 時,可以執行很多有用的參數,具體可以通過–help查看:
【或 http://nginx.org/en/docs/configure.html】
[test1280@test1280 nginx-1.15.10]$ ./configure --help
--help print this message
--prefix=PATH set installation prefix
--sbin-path=PATH set nginx binary pathname
--modules-path=PATH set modules path
--conf-path=PATH set nginx.conf pathname
--error-log-path=PATH set error log pathname
--pid-path=PATH set nginx.pid pathname
……
--with-http_ssl_module enable ngx_http_ssl_module
--with-http_stub_status_module enable ngx_http_stub_status_module
--without-http_rewrite_module disable ngx_http_rewrite_module
--with-debug enable debug logging
……
若在執行configure時遇到錯誤,請參見文末備註解決。
3.編譯
[test1280@test1280 nginx-1.15.10]$ make
4.安裝
[test1280@test1280 nginx-1.15.10]$ make install
5.添加nginx可執行文件到PATH路徑
[test1280@test1280 ~]$ echo 'export PATH=$HOME/nginx/sbin:$PATH' >> $HOME/.bash_profile
退出並重新登陸。
至此,已安裝nginx成功,下一步我們啓動nginx。
[test1280@test1280 ~]$ cd $HOME/nginx && ls -l
total 16
drwxrwxr-x. 2 test1280 test1280 4096 Nov 2 14:49 conf
drwxr-xr-x. 2 test1280 test1280 4096 Nov 2 14:49 html
drwxrwxr-x. 2 test1280 test1280 4096 Nov 2 14:49 logs
drwxrwxr-x. 2 test1280 test1280 4096 Nov 2 14:49 sbin
進入執行configure時–prefix指定的nginx安裝目錄,可以看到以上子目錄,見名知意。
我要將nginx啓動在8080端口,可以修改 $HOME/nginx/conf/nginx.conf 配置,listen填寫8080即可。
server {
listen 8080;
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:
[test1280@test1280 ~]$ nginx -c $HOME/nginx/conf/nginx.conf
nginx -c 是按照指定配置文件啓動nginx。
如果啓動 nginx 失敗,請參見文末備註解決。
訪問 nginx:
如果訪問 nginx 失敗,請參見文末備註解決。
nginx 負載均衡配置
nginx 監聽 8080 端口(收斂點),收到 HTTP 請求,將其負載均衡到兩個後臺服務地址:
服務器1 | 服務器2 |
---|---|
127.0.0.1:8081 | 127.0.0.1:8082 |
修改 $HOME/nginx/conf/nginx.conf 配置:
upstream test1280.me {
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
server {
listen 8080;
server_name localhost;
location / {
proxy_pass http://test1280.me;
}
}
其中,test1280.me 可以自定義,需要和對應的 server 中的 proxy_pass 一致。
nginx 常用指令:
命令 | 含義 |
---|---|
nginx -c $nginx.conf | 啓動 nginx,按照指定配置文件 |
nginx -s stop | 停止 nginx(粗暴版) |
nginx -s quit | 停止 nginx(優雅版) |
nginx -s reload | 重載 nginx 配置,動態刷新(服務不中斷) |
nginx -v | 輸出 nginx 版本號 |
nginx -V | 輸出 nginx 版本號,及configure時指定的參數信息 |
…… | …… |
[test1280@test1280 ~]$ nginx -h
nginx version: nginx/1.15.10
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /home/test1280/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
備註:
1)執行configure生成makefile時,出現錯誤:
錯誤:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
原因:
默認nginx支持rewrite模塊。
PCRE(Perl Compatible Regular Expressions,Perl兼容正則表達式)庫支持正則表達式。
Nginx的HTTP模塊要靠它來解析正則表達式。
解決:
以root權限安裝pcre-devel到系統
[root@test1280 ~]# yum install -y pcre-devel
錯誤:
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
原因:zlib庫用於對HTTP包的內容做gzip格式的壓縮。
解決:
以root權限安裝zlib-devel到系統
[root@test1280 ~]# yum install -y zlib-devel
錯誤:
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
原因:nginx要支持 HTTPS(SSL/TLS) ,需要加密算法庫支持,例如openssl庫。
解決:
以root權限安裝openssl-devel到系統
[root@test1280 ~]# yum install -y openssl-devel
2)啓動 nginx 失敗報錯:
錯誤:
[test1280@test1280 ~]$ nginx -c $HOME/nginx/conf/nginx.conf
nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
原因:
nginx.conf 中配置 listen 80,監聽 80 端口。
但是,僅有 root 用戶纔有權限監聽1024以下的端口。
我們是以 test1280 身份啓動監聽 80 端口,權限不足。
解決:
以 root 權限啓動 或 修改監聽 1024 以上端口。
錯誤:
[test1280@test1280 ~]$ nginx -c $HOME/nginx/conf/nginx.conf
nginx: [emerg] unexpected end of file, expecting ";" or "}" in /home/test1280/nginx/conf/nginx.conf:86
……
nginx: [emerg] unexpected "}" in /home/test1280/nginx/conf/nginx.conf:85
配置文件語法錯誤。
可以通過 nginx -t -c $nginx.conf 校驗配置文件語法:
[test1280@test1280 ~]$ nginx -t -c /home/test1280/nginx/conf/nginx.conf
nginx: [emerg] unexpected "}" in /home/test1280/nginx/conf/nginx.conf:85
nginx: configuration file /home/test1280/nginx/conf/nginx.conf test failed
3.訪問 nginx 失敗:
錯誤:瀏覽器訪問 http://192.168.20.131:8080/ “無法訪問此網站”。
原因:可能是服務器防火牆拒絕8080端口訪問。
解決:添加 8080 端口到 iptables 規則,開放8080端口。
https://blog.csdn.net/test1280/article/details/102875081
nginx 使用zlib、pcre、openssl庫源碼安裝
若configure使用操作系統自帶的zlib、pcre、openssl庫時,ldd可以看到衆多依賴:
[test1280@test1280 ~]$ ldd nginx/sbin/nginx
linux-vdso.so.1 => (0x00007ffd9a96a000)
libdl.so.2 => /lib64/libdl.so.2 (0x0000003da0a00000)
librt.so.1 => /lib64/librt.so.1 (0x0000003da1600000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003da1200000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x0000003dac600000)
libpcre.so.0 => /lib64/libpcre.so.0 (0x00007fbfec942000)
libssl.so.10 => /usr/lib64/libssl.so.10 (0x00000036ffc00000)
libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x00000036fe800000)
libz.so.1 => /lib64/libz.so.1 (0x0000003da2200000)
libc.so.6 => /lib64/libc.so.6 (0x0000003da0e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003da0600000)
libfreebl3.so => /lib64/libfreebl3.so (0x0000003dad200000)
libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00000036ff000000)
libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00000036fec00000)
libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00000036fe400000)
libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00000036ff400000)
libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00000036ff800000)
libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x0000003dafe00000)
libresolv.so.2 => /lib64/libresolv.so.2 (0x0000003da3200000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003da2600000)
執行configure指定依賴庫源文件,可以減少動態庫依賴。
-
zlib
https://zlib.net/
http://45.252.224.82/files/223900000DDAF9C0/zlib.net/zlib-1.2.11.tar.gz -
pcre
https://www.pcre.org/
https://ftp.pcre.org/pub/pcre/
https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz -
openssl
https://www.openssl.org/
https://www.openssl.org/source/openssl-1.0.2t.tar.gz
1.準備nginx源碼包、openssl、zlib、pcre源碼包:
[test1280@test1280 install]$ ls -l
total 8880
-rw-rw-r--. 1 test1280 test1280 1032228 Sep 3 05:08 nginx-1.15.10.tar.gz
-rw-r--r--. 1 test1280 test1280 5355422 Nov 2 08:16 openssl-1.0.2t.tar.gz
-rw-r--r--. 1 test1280 test1280 2085854 Nov 2 08:12 pcre-8.43.tar.gz
-rw-r--r--. 1 test1280 test1280 607698 Nov 2 08:10 zlib-1.2.11.tar.gz
2.解壓:
[test1280@test1280 install]$ tar zxf openssl-1.0.2t.tar.gz
tar zxf[test1280@test1280 install]$ tar zxf pcre-8.43.tar.gz
[test1280@test1280 install]$ tar zxf zlib-1.2.11.tar.gz
[test1280@test1280 install]$ tar zxf nginx-1.15.10.tar.gz
[test1280@test1280 install]$ cd nginx-1.15.10/
3.configure:
執行configure指定源碼位置:
[test1280@test1280 nginx-1.15.10]$ ./configure --prefix=$HOME/nginx \
> --with-http_ssl_module \
> --with-openssl=/home/test1280/install/openssl-1.0.2t \
> --with-pcre=/home/test1280/install/pcre-8.43 \
> --with-zlib=/home/test1280/install/zlib-1.2.11
結果:
Configuration summary
+ using PCRE library: /home/test1280/install/pcre-8.43
+ using OpenSSL library: /home/test1280/install/openssl-1.0.2t
+ using zlib library: /home/test1280/install/zlib-1.2.11
……
4.編譯 & 安裝:
[test1280@test1280 nginx-1.15.10]$ make && make install
5.查看:
ldd $HOME/nginx/sbin/nginx
[test1280@test1280 ~]$ ldd $HOME/nginx/sbin/nginx
linux-vdso.so.1 => (0x00007fff4c6f0000)
libdl.so.2 => /lib64/libdl.so.2 (0x0000003da0a00000)
librt.so.1 => /lib64/librt.so.1 (0x0000003da1600000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003da1200000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x0000003dac600000)
libc.so.6 => /lib64/libc.so.6 (0x0000003da0e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003da0600000)
libfreebl3.so => /lib64/libfreebl3.so (0x0000003dad200000)
nginx 依賴的動態庫少了許多。
使用依賴庫源碼安裝,編譯鏈接時間略長,相當於將第三方庫靜態編譯到nginx中。
有時,我們要安裝nginx的主機和公網不同,不便使用yum/apt等包管理工具安裝,使用源碼安裝是個不錯的選擇噢!
參考:
1.http://www.belonk.com/c/nginx_download_install.html
2.https://www.4spaces.org/nginx-tutorial/
3.https://www.runoob.com/linux/nginx-install-setup.html
4.https://www.cnblogs.com/taiyonghai/p/6728707.html
5.https://yq.aliyun.com/articles/614970