您的当前位置:首页>全部文章>文章详情

nginx常用命令

发表于:2019-12-30 16:25:25浏览:54次TAG: #Nginx

帮助文档

$ nginx -h
nginx version: openresty/1.5.12.1
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help                     // 帮助命令
  -v            : show version and exit          // 显示版本并退出
  -V            : show version and configure options then exit     // 显示版本并配置选项,然后退出
  -t            : test configuration and exit     // 测试配置并退出
  -q            : suppress non-error messages during configuration testing     // 在配置测试期间禁止显示非错误消息
  -s signal     : send signal to a master process: stop, quit, reopen, reload     // 向主进程发送信号:停止、退出、重新打开、重新加载
  -p prefix     : set prefix path (default: /usr/local/openresty/nginx/nginx/nginx/)     // 设置前缀路径
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)     // 设置配置文件
  -g directives : set global directives out of configuration file         // 从配置文件中设置全局指令

测试配置 [ -t ]

$ sudo nginx -t

输出 (正确):

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

向主进程发送信号 [ -s ]

# 停止服务
nginx -s stop

# 优雅停止nginx,有连接时会等连接请求完成再杀死worker进程
nginx -s quit

# 重启服务
nginx -s reload

# 重新打开日志文件,一般用于切割日志
nginx -s reopen

显示版本 [ -v ]

$ nginx -v

输出:

nginx version: openresty/1.5.12.1

显示版本并配置选项 [ -V ]

$ nginx -V

输出:

nginx version: openresty/1.5.12.1
built by gcc 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx/nginx/nginx --with-debug --add-module=../ngx_devel_kit-0.2.19 --add-module=../echo-nginx-module-0.53 --add-module=../xss-nginx-module-0.04 --add-module=../ngx_coolkit-0.2rc1 --add-module=../set-misc-nginx-module-0.24 --add-module=../form-input-nginx-module-0.08 --add-module=../encrypted-session-nginx-module-0.03 --add-module=../srcache-nginx-module-0.26 --add-module=../ngx_lua-0.9.7 --add-module=../ngx_lua_upstream-0.02 --add-module=../headers-more-nginx-module-0.25 --add-module=../array-var-nginx-module-0.03 --add-module=../memc-nginx-module-0.14 --add-module=../redis2-nginx-module-0.11 --add-module=../redis-nginx-module-0.3.7 --add-module=../auth-request-nginx-module-0.2 --add-module=../rds-json-nginx-module-0.13 --add-module=../rds-csv-nginx-module-0.05 --with-ld-opt='-Wl,-rpath,/usr/local/openresty/nginx/nginx/luajit/lib -Wl,-rpath,/usr/local/openresty/nginx/luajit/lib -Wl,-rpath,/usr/local/openresty/luajit/lib' --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --with-pcre-jit --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-mail --with-mail_ssl_module

将 nginx 加入到 service 中

sudo vim /etc/init.d/nginx

写入

#!/bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# 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

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx

# Include nginx defaults if available
if [ -f /etc/default/nginx ]; then
    . /etc/default/nginx
fi

test -x $DAEMON || exit 0

set -e

. /lib/lsb/init-functions

test_nginx_config() {
    if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
        return 0
    else
        $DAEMON -t $DAEMON_OPTS
        return $?
    fi
}

case "$1" in
    start)
        echo -n "Starting $DESC: "
        test_nginx_config
        # Check if the ULIMIT is set in /etc/default/nginx
        if [ -n "$ULIMIT" ]; then
            # Set the ulimits
            ulimit $ULIMIT
        fi
        start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
            --exec $DAEMON -- $DAEMON_OPTS || true
        echo "$NAME."
        ;;

    stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
            --exec $DAEMON || true
        echo "$NAME."
        ;;

    restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile \
            /var/run/$NAME.pid --exec $DAEMON || true
        sleep 1
        test_nginx_config
        start-stop-daemon --start --quiet --pidfile \
            /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
        echo "$NAME."
        ;;

    reload)
        echo -n "Reloading $DESC configuration: "
        test_nginx_config
        start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
            --exec $DAEMON || true
        echo "$NAME."
        ;;

    configtest|testconfig)
        echo -n "Testing $DESC configuration: "
        if test_nginx_config; then
            echo "$NAME."
        else
            exit $?
        fi
        ;;

    status)
        status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
        ;;
    *)
        echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
        exit 1
        ;;
esac

exit 0

设置可执行

sudo chmod +x /etc/init.d/nginx

相关命令

service nginx start | stop | restart | reload | status | help
栏目分类全部>
腾讯云采购季云服务器一折促销