1. 创建前端项目目录
进入 /var 文件夹
cd /var
在 /var 目录下创建 www 文件夹
mkdir www
进入 /var/www 目录,创建 test 文件夹
cd www #进入 www 目录
mkdir test
2. 将前端编译后的文件上传到 /var/www/test 目录
3. 配置 nginx 下 test 目录及选项
进入 /etc/nginx/conf.d/ 目录
cd /etc/nginx/conf.d/
打开 default.conf,写入 test 的相关配置
server {
listen 80; # nginx 监听端口
server_name localhost; #若有域名则将 localhost 替换为域名
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /var/www/test; #test 项目路径
index index.html index.htm; #默认起始页
try_files $uri $uri/ /index.html; #spa 前端项目路由配置
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {root /usr/share/nginx/html;}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
4. 测试部署
curl localhost #假设项目端口为 80 则为 localhost
如若返回的 html 字符串中没有异常报错的状态码则为成功
5. 错误问题排查
1)主机内使用 curl 可以访问,客户端机器无法访问
解决方法:检查当前主机防火墙是否开放监听的端口
查看当前主机开放的端口
firewall-cmd --list-ports
开放主机端口
firewall-cmd --add-port=80/tcp --permanent
关闭主机端口
firewall-cmd --remove-port=80/tcp --permanent
注意:执行完开放或者关闭端口后需要重启防火墙
firewall-cmd --reload
2)主机内外都无法访问页面,但都能访问 nginx 提示 403 forbidden 的错误页面
解决办法:可能为 SELinux 为开启的状态,关闭 SELinux 即可
查看 SELinux 状态
/usr/sbin/sestatus -v ## 如果 SELinux status 参数为 enabled 即为开启状态
临时关闭(不用重启机器,当前设置可用于验证是否为 SELinux 开启导致 403 的错误)
setenforce 0 ## 设置 SELinux 成为 permissive 模式
##setenforce 1 设置 SELinux 成为 enforcing 模式
永久关闭,编辑 SELinux(需要重启)
vi /etc/selinux/config # 将 SELINUX=enforcing 改为 SELINUX=disabled