因为前端工作缓和,帮忙部署下前端页面,早就听过nginx的小名,所以初步选定了nginx的计划

装置nginx

环境:CentOS 7.4
注:本文所有命令都是在root用户下执行的,如果是普通用户,请加sudo
装置很简略,间接一条命令搞定(不过yum装置的个别都不是最新版本,如果要装最新版本请自行下载安装):

yum install -y nginx

前提是能够连外网,如果连不了外网,能够用其余形式将安装包下载下载安装,这里就不形容了,网上有很多帖子能够参考。

配置nginx

如果不晓得nginx的配置文件门路,能够用如下命令查看:

nginx -t[root@local]# nginx -tnginx: the configuration file etcnginxnginx.conf syntax is oknginx: configuration file etcnginxnginx.conf test is successful

关上配置文件etcnginxnginx.conf
批改对应的server模块内容

    server {        listen       8080 default_server;        #listen       [::]:80 default_server;        server_name  _;        #root         usrsharenginxhtml;        root          homefrontdist;        # Load configuration files for the default server block.        include etcnginxdefault.d*.conf;        location  {        root          homefrontdist;        }        error_page 404 404.html;            location = 40x.html {        }        error_page 500 502 503 504 50x.html;            location = 50x.html {        }    }

次要批改点:
1、端口,这个可选的,默认80,这里改为了8080
2、ipv6地址监听地址被正文掉了,这个随便,依据需要来
3、server_name应该是对应域名,这里没有用到,没有动
4、root homefrontdist;这个示意web的拜访目录,须要改为本人的web页面所在目录
5、include没有改变,也没钻研其作用是啥
6、location中减少了root homefrontdist;
7、error_page就用了nginx默认的,没有批改

改好之后保留退出

启动nginx

执行命令

[root@local]# nginx

而后拜访页面,发现报错:

最开始始终认为配置有问题,又搜查了各种配置计划,发现都无法访问,最初找到了一个解决方案:

[root@local homefront]# vi etcnginxnginx.conf# For more information on configuration, see:#   * Official English Documentation: http:nginx.orgendocs#   * Official Russian Documentation: http:nginx.orgrudocsuser nginx;

是因为权限问题,我是用root用户启动的nginx,然而配置中的用户默认的是nginx,所以将其user nginx改为root即可。
从新加载配置,并启动nginx

nginx -s reload

功败垂成。
不过这都是本地拜访用,没有用平安连贯,如果要用平安连贯,须要配置证书。同时这里应用的是http 1.1,如果要应用http 2.0协定,也须要配置http2.

nginx常用命令

应用nginx帮忙能够查看

[root@local homefront]# nginx -hnginx version: nginx1.16.1Usage: nginx [-?hvVtTq] [-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  -T            : test configuration, dump it 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: usrsharenginx)  -c filename   : set configuration file (default: etcnginxnginx.conf)  -g directives : set global directives out of configuration file

罕用的:
nginx -t :上文已用过,次要是测试配置文件是否有语法错误
nginx -s :次要是向nginx主过程发送信号进行解决,帮忙信息外面也列出了信号名称:stop, quit, reopen, reload

nginx -s stop:强制进行Nginx服务
nginx -s quit:优雅地进行Nginx服务(即解决完所有申请后再进行服务)
nginx -s reopen:重启Nginx
nginx -s reload:从新加载Nginx配置文件,而后以优雅的形式重启Nginx

参考

https:blog.csdn.netqq_3584...