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