每个Linux用户必须知道的10个最常用的Nginx命令(2)

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main;

sendfile            on;
    tcp_nopush          on;
    tcp_nodelay        on;
    keepalive_timeout  65;
    types_hash_max_size 2048;

include            /etc/nginx/mime.types;
    default_type        application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
    # See #include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

server {
        listen      80 default_server;
        listen      [::]:80 default_server;
        server_name  _;
        root        /usr/share/nginx/html;

# Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

location / {
        }

error_page 404 /404.html;
            location = /40x.html {
        }

error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

....

启动Nginx服务

要启动Nginx服务,请运行以下命令。 请注意,如果配置语法不正确,此过程可能会失败。

$ sudo systemctl start nginx #systemd
或者
$ sudo service nginx start  #sysvinit

启用Nginx服务

上一个命令仅在此期间启动服务,要在启动时启用它自动启动,请运行以下命令。

$ sudo systemctl enable nginx #systemd
或者
$ sudo service nginx enable  #sysv init

重启Nginx服务

要重新启动Nginx服务,将停止然后启动该服务的操作。

$ sudo systemctl restart nginx #systemd
或者
$ sudo service nginx restart  #sysv init

查看Nginx服务状态

您可以按如下方式检查Nginx服务的状态。 此命令显示有关服务的运行时状态信息。

$ sudo systemctl status nginx #systemd
或者
$ sudo service nginx status  #sysvinit

显示Nginx状态信息

Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@linuxidc ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
  Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
  Active: active (running) since Tue 2019-03-05 05:27:15 EST; 2min 59s ago
 Main PID: 31515 (nginx)
  CGroup: /system.slice/nginx.service
          ├─31515 nginx: master process /usr/sbin/nginx
          └─31516 nginx: worker process

Mar 05 05:27:15 linuxidc.com systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 05 05:27:15 linuxidc.com nginx[31509]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Mar 05 05:27:15 linuxidc.com nginx[31509]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 05 05:27:15 linuxidc.com systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Mar 05 05:27:15 linuxidc.com systemd[1]: Started The nginx HTTP and reverse proxy server.

重新加载Nginx服务

要告诉Nginx重新加载其配置,请使用以下命令。

$ sudo systemctl reload nginx #systemd
或者
$ sudo service nginx reload  #sysvinit

停止Nginx服务

如果您想出于一次性原因而停止Nginx服务,请使用以下命令。

显示Nginx命令帮助

要获得所有Nginx命令和选项的简单参考指南,请使用以下命令。

$ systemctl -h nginx

Nginx帮助命令和选项

systemctl [OPTIONS...] {COMMAND} ...

Query or send control commands to the systemd manager.

-h --help          Show this help
    --version        Show package version
    --system        Connect to system manager
  -H --host=[USER@]HOST
                      Operate on remote host
  -M --machine=CONTAINER
                      Operate on local container
  -t --type=TYPE      List units of a particular type
    --state=STATE    List units with particular LOAD or SUB or ACTIVE state
  -p --property=NAME  Show only properties by this name
  -a --all            Show all loaded units/properties, including dead/empty
                      ones. To list all units installed on the system, use
                      the 'list-unit-files' command instead.
  -l --full          Don't ellipsize unit names on output
  -r --recursive      Show unit list of host and local containers
    --reverse        Show reverse dependencies with 'list-dependencies'
    --job-mode=MODE  Specify how to deal with already queued jobs, when
                      queueing a new job
    --show-types    When showing sockets, explicitly show their type
  -i --ignore-inhibitors
...

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/6330ef901cd9e2635dd756d0ea21afdc.html