基于Centos7.2的Nginx部署
部署步骤:
安装zlib-devel、pcre-devel等依赖包
[root@Nginx ~]# yum install -y gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel
注:结合proxy和upstream模块实现后端web负载均衡
结合Nginx默认自带的ngx_http_proxy_module模块 和ngx_http_upstream_module模块实现后端服务器的健康检查。
Proxy:实现反向代理
Upstream:实现负载均衡
[root@Nginx ~]# useradd -s /sbin/nologin www
[root@Nginx ~]# grep www /etc/passwd ##查看Nginx用户www是否建立
www:x:1000:1000::/home/www:/sbin/nologin
编译安装Nginx
[root@Nginx src]# tar -zxvf Nginx-1.13.0.tar.gz
[root@Nginx src]# cd Nginx-1.13.0
[root@Nginx Nginx-1.13.0]# ./configure --prefix=/usr/local/Nginx1.10 --user=www --group=www --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_flv_module && make && make install
[root@Nginx Nginx-1.13.0]# ln -s /usr/local/Nginx1.10/sbin/Nginx /usr/local/sbin/
Nginx语法检查
编写Nginx服务脚本
[root@Nginx ~]# vim /etc/init.d/Nginx
#!/bin/sh
#
# Nginx - this script starts and stops the Nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server,HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: Nginx
# config: /usr/local/Nginx1.10/conf/Nginx.conf
# pidfile: /usr/local/Nginx1.10/logs/Nginx.pid
Nginxd=/usr/local/Nginx1.10/sbin/Nginx
Nginx_config=/usr/local/Nginx1.10/conf/Nginx.conf
Nginx_pid=/usr/local/Nginx1.10/logs/Nginx.pid
RETVAL=0
prog="Nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
prog=$(basename $Nginx)
Nginx_CONF_FILE="/usr/local/Nginx/conf/Nginx.conf"
lockfile=/var/lock/subsys/Nginx
start() {
[ -x $Nginx ] || exit 5
[ -f $Nginx_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $Nginx -c $Nginx_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $Nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
添加开机自启动服务
[root@Nginx ~]# chmod +x /etc/init.d/Nginx
[root@Nginx ~]# chkconfig --add Nginx
[root@Nginx ~]# chkconfig Nginx on
[root@Nginx ~]# chkconfig --list |grep Nginx
Nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
启动Nginx服务
[root@Nginx ~]# /usr/local/sbin/Nginx start
Nginx: invalid option: "start"
[root@Nginx ~]# /etc/init.d/Nginx start
Starting Nginx (via systemctl): Job for Nginx.service Failed because the control process exited with error code. See "systemctl status Nginx.service" and "journalctl -xe" for details.
[Failed]
[root@Nginx ~]# /usr/local/sbin/Nginx
[root@Nginx ~]# /etc/init.d/Nginx start
Starting Nginx (via systemctl): [ OK ]
配置Nginx反向代理:作用是(反向代理+负载均衡+健康探测)
[root@Nginx ~]# vim /usr/local/Nginx1.10/conf/Nginx.conf
user www www;
worker_processes 2;
worker_cpu_affinity 0101 1010;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
worker_rlimit_nofile 10240;
pid logs/Nginx.pid;
events{
use epoll;
worker_connections 4096;
}
http{
include mime.types;
default_type application/octet-stream;
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;
server_tokens off;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#Compression Settings
gzip on;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_proxied any;
gzip_min_length 1k;
gzip_buffers 16 8k;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascriptapplication/xml;
gzip_vary on;
#end gzip
# http_proxy Settings
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
#load balance Settings
upstream backend_tomcat {
server 192.168.100.126:8080 weight=1 max_fails=2 fail_timeout=10s; ##需要更改为tomcat的ip
server 192.168.100.127:8080 weight=1 max_fails=2 fail_timeout=10s; ##需要更改为tomcat的ip
}
#virtual host Settings
server{
listen 80;
server_name www.benet.com;
charset utf-8;
location / {
root html;
index index.jsp index.html index.htm;
}
location ~* \.(jsp|do)$ {
proxy_pass http://backend_tomcat;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
location /Nginx_status {
stub_status on;
access_log off;
allow 192.168.100.0/24; ##需要更改tomcat的ip段
deny all;
}
}
}
重启使其生效
[root@Nginx conf]# /usr/local/sbin/Nginx
[root@Nginx conf]# service Nginx restart
Restarting Nginx (via systemctl): [ OK ]
[root@Nginx ~]# firewall-cmd --permanent --add-port=80/tcp
success
[root@Nginx ~]# firewall-cmd --reload
success
以上就是Nginx部署的基本步骤!