一,系统准备工作
---------- 1.1 查找系统中是否有nginx 文件
~]# find / -name nginx
---------- 1.2 安装nginx得依赖包
~]# yum -y install openssl openssl-devel pcre pcre-devel gcc-c++
~]# rpm -qa openssl openssl-devel pcre pcre-devel gcc-c++
pcre-8.32-17.el7.x86_64
pcre-devel-8.32-17.el7.x86_64
openssl-devel-1.0.2k-21.el7_9.x86_64
openssl-1.0.2k-21.el7_9.x86_64
gcc-c++-4.8.5-44.el7.x86_64
---------- 1.3 基础目录、用户的创建以及二进制文件的下载
~]# mkdir -p /home/renyong/tools
~]# cd /home/renyong/tools
tools]# useradd -s /sbin/nologin -M nginx
tools]# wget http://nginx.org/download/nginx-1.6.3.tar.gz
tools]# tar xf nginx-1.6.3.tar.gz
tools]# cd nginx-1.6.3
nginx-1.6.3]# ./configure --user=nginx --group=nginx \
> --prefix=/application/nginx-1.6.3 --with-http_stub_status_module --with-http_ssl_module
nginx-1.6.3]# make && make install
nginx-1.6.3]# ln -s /application/nginx-1.6.3 /application/nginx
二, 在后端 web服务器上得操作
---------- 2.1 修改nginx配置文件
两个重点:
一,创建两个虚拟机(两个server),
二,定义日志格式'"$http_user_agent" "$http_x_forwarded_for"'
nginx-1.6.3]# echo > /application/nginx/conf/nginx.conf
nginx-1.6.3]# vi /application/nginx/conf/nginx.conf
nginx-1.6.3]# cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
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"'; #######
sendfile on;
keepalive_timeout 65;
##此处创建一个虚拟机bbs.etiantian.org
server { #####################
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
access_log logs/access_bbs.log main;
}
##此处创建另一个虚拟机www.etiantian.org
server { ###########################
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.html;
}
access_log logs/access_www.log main;
}
}
---------- 2.2 检查语法以及重启、停止、重新加载配置文件等…
------------检查配置文件语法是否正确 -t
nginx-1.6.3]# /application/nginx/sbin/nginx -t -------检查配置文件语法是否正确
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
------------加载修改后得配置文件
nginx-1.6.3]# /application/nginx/sbin/nginx -s reload
------------启动nginx
nginx-1.6.3]# /application/nginx/sbin/nginx
nginx-1.6.3]# netstat -lntup|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14912/nginx: master
------------停止nginx
nginx-1.6.3]# /application/nginx/sbin/nginx -s stop
nginx-1.6.3]# netstat -lntup|grep nginx
---------- 2.3 创建web访问目录
nginx-1.6.3]# mkdir /application/nginx/html/{www,bbs}
nginx-1.6.3]# for dir in www bbs;
> do echo "`ifconfig ens33|grep "10.4.7.[12]"|awk '{print $2}'`" $dir > /application/nginx/html/$dir/index.html ;
> done
nginx-1.6.3]# for dir in www bbs; do cat /application/nginx/html/$dir/index.html ;done
10.4.7.22 www
10.4.7.22 bbs
三,在反向代理服务器上得操作
---------- 3.1 修改配置文件(需要注意两处三点)
一处: ##在http花括号中创建
upstream 服务池的创建
二处: ##在server花括号中创建
第一点 proxy_pass # 指向要访问得服务池
第二点 proxy_set_heard Host $host; # 指定代理向后端发送http得host字段
nginx-1.6.3]# echo > /application/nginx/conf/nginx.conf
nginx-1.6.3]# vi /application/nginx/conf/nginx.conf
nginx-1.6.3]# cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
##这项尤为重要 创建服务池
upstream www_server_pools {
server 10.4.7.21 weight=1;
server 10.4.7.22 weight=1;
}
server {
listen 80;
server_name www.etiantian.org;
location / {
#将请求转发到对应的服务池里
proxy_pass http://www_server_pools;
#在代理向后端发送的http请求中加入host字段,节点服务器多虚拟主机的关键配置。
proxy_set_header Host $host;
}
}
}
---------- 3.2 检查语法以及重启、停止、重新加载配置文件等…
具体得参照2-2 步骤中得操作
四,测试效果,在windows 客户端做个简单得域名解析
---------- 4.1 在hosts文件中添加映射关系
1, 进入目录中
C:\Windows\System32\drivers\etc
2,打开hosts文件,添加以下映射关系并保存
10.4.7.11 www.etiantian.org
10.4.7.11 bbs.etiantian.org
---------- 4.2 在浏览器上输入www.etiantian.org
每刷新一下浏览器,对应得内容也会在两个后端服务器上来回切换
---------- 4.3 在浏览器上输入bbs.etiantian.org
每刷新一下浏览器,对应得内容也会在两个后端服务器上来回切换
五,创建启停重启脚本
为了便于以后得管理,在二进制包安装完成后创建一个脚本
(脚本简介,基于步骤2.2 创建了6个函数,然后用case语句 )
vi /usr/local/share/nginx.sh
#!/bin/bash
RETVAL=0
NGINX=/application/nginx/sbin/nginx
#启动函数
start() {
$NGINX
echo "Starting nginx-1.6.3..."
return $RETVAL
}
#停止函数
stop() {
$NGINX -s stop
echo "Stopping nginx-1.6.3..."
return $RETVAL
}
#重新加载配置文件
reload() {
$NGINX -s reload
echo "Reloading nginx-1.6.3..."
return $RETVAL
}
#平稳退出,可以使当前的worker processes处理完当前请求再退出
quit() {
$NGINX -s quit
echo "Smooth nginx-1.6.3..."
return $RETVAL
}
#restart
restart() {
$NGINX -s quit && $NGINX
echo "Smooth nginx-1.6.3..."
return $RETVAL
}
#检查语法是否正确
check() {
$NGINX -t
echo "check deplay nginx-1.6.3..."
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
quit)
quit
;;
restart)
restart
;;
check)
check
;;
*)
printf "nginx service: Usage: $0 {start|stop|reload|quit|restart|check}"
esac
#临时 添加环境变量
~]# export PATH=$PATH:/usr/local/share/
##永久添加环境变量
~]# vi /etc/profile.d/nginx.sh
NGINX=/usr/local/share/
PATH=$NGINX:$PATH
export NGINX PATH
~]# chmod 755 /usr/profile.d/nginx.sh
##系统便可以向下面这样操作了
~]# nginx.sh reload
Reloading nginx-1.6.3...
~]# nginx.sh stop
Stopping nginx-1.6.3...
~]# nginx.sh start
Starting nginx-1.6.3...
六,用url中目录地址实现代理转发
---------- 6.1 代理服务器nginx配置文件
修改部分是location得匹配项 为 /static/
~]# /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream static_pools {
server 10.4.7.21 weight=1;
server 10.4.7.22 weight=1;
}
server {
listen 80;
server_name www.etiantian.org;
location /static/ {
proxy_pass http://static_pools;
proxy_set_header Host $host;
}
}
}
---------- 6.2 对应在后端服务器创建web目录
在web1上
~]# cd /application/nginx/html/www/
~]# mkdir static
static]# echo "static_pools-21" > static/index.html
static]# curl http://www.etiantian.org/static/index.html
在web2上
~]# cd /application/nginx/html/www/
~]# mkdir static
static]# echo "static_pools-22" > static/index.html
static]# curl http://www.etiantian.org/static/index.html
---------- 6.3 加载配置文件,启动nginx服务
参照步骤2.2
---------- 6.4 测试,在windows上
NGINX 反向代理服务得简单应用到此结束