使用Supervisor和NGINX部署

本文介绍了如何安装和配置Supervisor来管理服务,并详细阐述了Nginx中`include proxy_params`的作用,它包含了常用proxy_set_header指令,用于在反向代理时传递客户端信息给后端服务器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Supervisor

sudo apt-get -y update && sudo apt-get install -y python-setuptools
sudo apt-get install supervisor
sudo mkdir /etc/supervisor
echo_supervisord_conf >  /etc/supervisor/supervisord.conf
# 上面文件中添加下面内容
[include]
files=conf.d/*.conf
# 若上面语句错误,则需切换到root用户,使用sudo可能也会报错
sudo su
# 想要加配置文件,在confd.d文件夹中添加.conf文件
sudo mkdir /etc/supervisor/conf.d

# 以下管理supervisor
# 添加service文件
sudo vim /etc/systemd/system/supervisord.service
# 激活supervisor
sudo systemctl start supervisord.service
# 查看状态
sudo systemctl status supervisord.service
# 修改完配置文件之后,需要执行
sudo supervisorctl reread
sudo supervisorctl update
# 进行supervisor的shell
sudo supervisorctl
# supervisord.service内容
[Unit]
Description=Supervisor daemon
Documentation=http://supervisord.org
After=network.target

[Service]
ExecStart=/usr/local/bin/supervisord -n -c /etc/supervisor/supervisord.conf
ExecStop=/usr/local/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/local/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
Alias=supervisord.service

NGINX

# 简单配置负载均衡,数值越大调用次数越多
upstream group1 {
	server 127.0.0.1:18080 weight=1;
	server 127.0.0.1:18081 weight=5;
    	}


    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass   http://group1/;
        }
		# search files in path of /home/plan/Pictures/ 
        location /static/ {
            alias   /home/plan/Pictures/;
            # 自动设置该路由指定文件夹下有index.html文件作为首页,如果没有index.html文件返回改文件夹下的文件名,类似ftp服务器
            # 若未配置下参数,则访问该路由报错
            autoindex on;
        }
        
        #  search files in path of /home/plan/Pictures/ 
        location /Pictures/ {
            root   /home/plan/;
            index index.html;
        }

nginx中include proxy_params作用

在Nginx配置中,include指令用于包��其他配置文件。proxy_params通常是一个预定义的参数文件,它包含了一组常用的proxy_set_header指令。

proxy_set_header指令用于修改从Nginx到后端服务器的请求头。这对于许多反向代理的场景是必要的,例如,让后端应用知道客户端的IP地址或原始请求的主机名。

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;


这些指令的作用如下:

Host $http_host;:将原始请求的Host头(包括端口,如果有的话)传递给后端服务器。
X-Real-IP $remote_addr;:将客户端的真实IP地址传递给后端服务器。
X-Forwarded-For $proxy_add_x_forwarded_for;:将客户端的真实IP地址添加到X-Forwarded-For头中,如果
该头已经存在,Nginx会将新的IP地址添加到该头的末尾。
X-Forwarded-Proto $scheme;:将原始请求的协议(http或https)传递给后端服务器。

# 开机自启
# 编写文件加入下属内容
sudo vim /etc/init.d/nginx
chmod +x /etc/init.d/nginx
# 执行下面语句只重新加载配置,重启启动worker,master进程还存活。
sudo kill -HUP <pid>
/etc/init.d/nginx restart #这个管理脚本支持的命令有start|stop|force-quit|restart|reload|status|configtest
#开机自启
sudo update-rc.d nginx defaults
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

# Author:   licess
# website:  http://lnmp.org

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid

case "$1" in
    start)
        echo -n "Starting $NAME... "

        if netstat -tnpl | grep -q nginx;then
            echo "$NAME (pid `pidof $NAME`) already running."
            exit 1
        fi

        $NGINX_BIN -c $CONFIGFILE

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    stop)
        echo -n "Stoping $NAME... "

        if ! netstat -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        $NGINX_BIN -s stop

        if [ "$?" != 0 ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
        ;;

    status)
        if netstat -tnpl | grep -q nginx; then
            PID=`pidof nginx`
            echo "$NAME (pid $PID) is running..."
        else
            echo "$NAME is stopped"
            exit 0
        fi
        ;;

    force-quit)
        echo -n "Terminating $NAME... "

        if ! netstat -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        kill `pidof $NAME`

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    restart)
        $0 stop
        sleep 1
        $0 start
        ;;

    reload)
        echo -n "Reload service $NAME... "

        if netstat -tnpl | grep -q nginx; then
            $NGINX_BIN -s reload
            echo " done"
        else
            echo "$NAME is not running, can't reload."
            exit 1
        fi
        ;;

    configtest)
        echo -n "Test $NAME configure files... "

        $NGINX_BIN -t
        ;;

    *)
        echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
        exit 1
        ;;

esac

# 安装完nginx后一些提示文件的路径
Configuration summary
  + using PCRE library: /usr/local/lib
  + using system OpenSSL library
  + using system zlib library
  + jemalloc library is disabled

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/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"

链接: Supervisor安装.
链接: 被问懵逼:谈谈 Nginx 快的原因?.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值