nginx部署网站

7次阅读

共计 2251 个字符,预计需要花费 6 分钟才能阅读完成。

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

装置 nginx

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

yum install -y nginx

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

配置 nginx

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

nginx -t
[root@local]# nginx -t
nginx: the configuration file etcnginxnginx.conf syntax is ok
nginx: 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.orgrudocs

user nginx;

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

nginx -s reload

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

nginx 常用命令

应用 nginx 帮忙能够查看

[root@local homefront]# nginx -h
nginx version: nginx1.16.1
Usage: 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...

正文完
 0