nginx反向代理脚本配置
Nginx反向代理配置脚本 通过脚本参数实现Nginx反向代理的Server模块配置,配置格式如下
# 默认域名反向代理配置
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass http://www.example.com;
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;
access_log logs/www.example.com main;
}
}
upstream www.example.com {
server 10.0.0.1:80;
server 10.0.0.2:80;
}
# 默认域名及路径反向代理配置
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass http://www.example.com;
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;
access_log logs/www.example.com main;
}
location ^~ /directory/ {
proxy_pass http://www.example.com.directory;
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;
access_log logs/www.example.com.directory main;
}
}
upstream www.example.com {
server 10.0.0.1:80;
server 10.0.0.2:80;
}
upstream www.example.com.directory {
server 10.0.0.3:80;
server 10.0.0.4:80;
}
使用说明
# 需修改的脚本内容
bin=/usr/local/nginx/sbin/nginx #Nginx可执行程序路径
conf=/usr/local/nginx/conf/nginx.conf #Nginx主配置文件路径
conf_directory=/usr/local/nginx/conf/vhost #Nginx虚拟站点配置文件目录,在主配置文件中使用"include vhost/*.conf;"包含
bak_directory=/root/nginx_backup #Nginx备份文件路径
使用方法
nginx-cfg -t [type] -s [server_name] -h [ip:port] -d [directory]
参数说明
-t [type] :脚本执行的模式
add|a 添加域名或目录映射(默认模式)
modify|m 修改域名的upstream映射配置
delete|d 删除域名或目录映射
search|s 查看指定域名或目录的映射
-s [server_name] :配置的域名,例如:www.yihu.com
-h [ip:port] :映射的主机地址及端口,多条记录以‘,’隔开,例如:’10.0.0.1:80,10.0.0.2:80’
-d [directory] :location目录
使用案例
1、添加一个域名www.example.com,映射到主机10.0.0.1,端口80
nginx-cfg -t add -s www.example.com -h 10.0.0.1:80
2、添加一个域名www.example.com,映射到主机10.0.0.2和10.0.0.3,端口都为8080
nginx-cfg -t add -s www.example.com -h “10.0.0.2:8080,10.0.0.3:8080”
3、添加一个目录映射,域名为www.example.com,目录为directory,映射到主机10.0.0.4:80和10.0.0.5:8080
nginx-cfg -t add -s www.example.com -d directory -h “10.0.0.4:80,10.0.0.5:8080”
4、修改一个域名的映射,现有www.example.com映射至10.0.0.1:80,现在要加一台负载10.0.0.2:80
nginx-cfg -t modify -s www.example.com -h “+10.0.0.2:80”
5、修改一个域名的映射,现有www.example.com映射至10.0.0.1:80和10.0.0.2:80,现在要删一台负载10.0.0.1:80
nginx-cfg -t modify -s www.example.com -h “-10.0.0.1:80”
6、修改一个域名的映射,现有www.example.com,目录为directory,映射至10.0.0.2:80,现在要替换映射为10.0.0.3:80和10.0.0.4:80
nginx-cfg -t modify -s www.example.com -d directory -h “=10.0.0.3:80,10.0.0.4:80”
7、删除域名www.example.com下所有映射
nginx-cfg -t delete -s www.example.com
8、删除域名www.example.com下目录directory的映射
nginx-cfg -t delete -s www.example.com -d directory
9、查看域名www.example.com对应的映射
nginx-cfg -t search -s www.example.com
10、查看域名www.example.com下目录directory对应的映射
nginx-cfg -t search -s www.example.com -d directory
下面的脚本本人生产用的
#!/bin/bash
#MODIFY:
if [ `whoami` != "root" ];then
echo "This script must be runing as root!!!"
exit 71
fi
export LC_ALL=en_US.UTF-8
current_dir=$(cd `dirname $0` && pwd) #当前工作目录
nginx_path="/usr/local/nginx/conf" #或者为/usr/local/nginx
cecho(){
echo -e "\033[$1m$2\033[0m"
}
#配置Nginx主配置文件
Conf_Master(){
[ ! -f ${nginx_path}/nginx.conf ] && cecho "Nginx路径错误,请修改脚本" && exit 71
[ ! -f ${nginx_path}/nginx.conf.bak ] && cp ${nginx_path}/nginx.conf{,.bak}
cat > ${nginx_path}/nginx.conf << EOF
user nginx;
worker_processes auto;
error_log off;
#pid logs/nginx.pid;
events {
worker_connections 30000;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
#tcp_nodelay on;
keepalive_timeout 65;
server_names_hash_bucket_size 64;
server_names_hash_max_size 2048;
server_tokens off;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
#access_log /var/log/nginx/access.log;
access_log off;
#error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /usr/local/nginx/conf.d/*.conf;
}
EOF
}
#配置反向代理
Conf_Proxy(){
[ ! -d ${nginx_path}/conf.d ] && mkdir ${nginx_path}/conf.d
#写反向代理配置文件
cat > ${nginx_path}/proxy.conf << EOF
proxy_redirect off;
proxy_set_header Host \$host:\$server_port;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header REMOTE-HOST \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
client_max_body_size 1200m;
client_body_buffer_size 256k;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_max_temp_file_size 500m;
EOF
#配置server
cat > ${nginx_path}/conf.d/down.conf << EOF
server {
listen ${listen_port};
server_name localhost;
client_max_body_size 500m; #上传下载文件大小
location / {
include ${nginx_path}/proxy.conf;
proxy_pass ${proxy_path};
}
}
EOF
}
#打印脚本说明
Print(){
cat << EOF
#################################################################################
#1、AUTHOR:qixiang.an@bangcle.com
#2、DATE:2020-01-11
#3、功能:配置Nginx反向代理
#4、注意事项:nginx的默认路径为/etc/nginx,如果不是请修改"nginx_path"变量
#################################################################################
EOF
}
#主函数
main(){
Print
cecho 32 "开始执行......"
echo "1.未配置${nginx_path}nginx.conf"
echo "2.已配置${nginx_path}nginx.conf"
read -p "请选择:" choice
read -p "监听的端口:" listen_port
# read -p "server_name:" server_name
read -p "代理访问的地址:" proxy_path
if [ "${choice}" -eq 1 ];then
Conf_Master
Conf_Proxy
else
Conf_Proxy
fi
cecho 96 "配置完成,请用nginx -t检查语法是否正确,如果正确,执行nginx -s reload使配置文件生效"
}
main