Nginx官方文档:https://www.nginx.com/resources/wiki/start/
依赖包可在此网站下搜索:http://vault.centos.org/7.4.1708/os/x86_64/Packages/
cmake-2.8.12.2-2.el7.x86_64.rpm
make-3.82-24.el7.x86_64.rpm
gcc-4.8.5-39.el7.x86_64.rpm
gcc-c++-4.8.5-39.el7.x86_64.rpm
pcre-devel-8.32-17.el7.x86_64.rpm
pcre2-10.23-2.el7.x86_64.rpm
zlib-devel-1.2.7-18.el7.x86_64.rpm
openssl-devel-1.0.2k-8.el7.x86_64.rpm
nginx-1.6.2.tar.gz
mkdir /usr/local/software
cd /usr/local/software
rpm -Uvh *.rpm --nodeps
这里已经安装过了,yum安装执行
yum -y install cmake make gcc gcc-c++ openssl-devel pcre-devel pcre2 zlib-devel
tar zxvf nginx-1.6.2.tar.gz -C /usr/local/
cd /usr/local/nginx-1.6.2/ && ./configure --prefix=/usr/local/nginx
make && make install
cd /usr/local/nginx && ls
/usr/local/nginx/sbin/nginx
ps -ef | grep nginx
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-ports
如果访问出现 403 Forbiden ,需要修改配置文件
vim /usr/local/nginx/conf/nginx.conf
在默认配置 #user nobody; 下添加
user root;
赋予 /usr/local/nginx/html/index.html 文件 644权限
chmod 644 /usr/local/nginx/html/index.html
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: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
# nginx="/usr/sbin/nginx"
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
# NGINX_CONF_FILE="/etc/nginx/nginx.conf"
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
fi
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $prog -HUP
retval=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
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
给 /etc/init.d/nginx 文件赋予执行权限
chmod +x /etc/init.d/nginx
创建pid文件
echo 10000 > /var/run/nginx.pid
将启动脚本配置到chkconfig服务
chkconfig --add /etc/init.d/nginx
chkconfig --list
开机自启
chkconfig nginx on
或者在/etc/rc.d/rc.local文件下添加
/usr/local/nginx/sbin/nginx
启动和关闭命令
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx -s reload
service nginx status
service nginx start
service nginx stop
service nginx restart
systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
./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=option.
解决:以下依赖,如果需要其他依赖则使用 --nodeps 不依靠依赖安装
rpm -Uvh pcre-devel-8.32-17.el7.x86_64.rpm
rpm -Uvh pcre2-10.23-2.el7.x86_64.rpm
rpm -Uvh zlib-devel-1.2.7-18.el7.x86_64.rpm
rpm -Uvh openssl-devel-1.0.2k-8.el7.x86_64.rpm
正向代理(爬虫代理):客户端(浏览器)通过代理服务器(配置代理服务器)进行互联网访问
反向代理(服务代理):客户端将请求发送给反向代理服务器,反向代理再获取指定目标服务器数据再返回给客户端(客户端只认识代理服务器ip)
正向代理隐藏真实客户端,反向代理隐藏真实服务器
正向代理 代理用户,反向代理 代理服务器
反向代理示例一:
如下修改nginx配置文件 nginx/conf/nginx.conf
server模块修改代理服务器ip:
server_name 192.168.0.130;server模块下的location模块添加代理ip:
proxy_pass http://192.168.0.180:8080;
nginx服务器ip:192.168.0.130 监听端口:80
tomcat服务器:192.168.0.180 监听端口:8080
http://192.168.0.130/ http://192.168.0.180:8080/
此时,访问tomcat服务器可正常访问tomcat页面,访问nginx服务器ip可代理转发到tomcat页面
反向代理示例二:
使用nginx监听两个tomato不同端口,对外开放一个端口,通过ip名称正则表达式区分
修改nginx配置文件 nginx/conf/nginx.conf
server {
listen 9090;
server_name 192.168.0.130;
location / {
proxy_pass http://192.168.0.130:8085;
}
location ~ /henan/ {
proxy_pass http://192.168.0.130:8089;
}
}
在本机同时启动两个tomcat,修改端口分别为 8085和8089,然后使用nginx监听这两个端口
部署多个tomcat主要修改三个端口(tomcat/conf/server.xml):
1.HTTP端口,默认8080,两个tomcat分别修改为 8085 和 8089
2.远程停服务端口,默认8005,另一个修改为 8006
3.AJP端口,默认8009,另一个修改下改 8010
在 8089端口的tomcat目录 webapps目录下新建目录 henan,然后在该目录下新建 index.html 文件,添加代码
tomcat9
分别启动两个tomcat,然后重启nginx,浏览器访问
http://192.168.0.130:8085/
http://192.168.0.130:9090/henan/
访问效果