问题1:
描述:前段时间遇到一个奇葩的问题,用nginx部署了前端网站,正常的html网站是可以访问的。但是部署vue的项目时确忽然报错。报的还是语法错误,如下:
Uncaught SyntaxError: Unexpected token '<'
配置如下:
server {
listen 7081;#监听端口
server_name localhost;#监听域名
charset utf-8;#指定网页的编码格式
location / {
root /usr/share/nginx/html/musicapp;
index index.html;
try_files $uri $uri/ /index.html;
}
}
解决方案:
第一步:打开安装目录下的nginx.conf文件,将第一行user xxxx修改为root用户即可。
user root;
第二步:修改目录及文件权限,使得所有文件是 644 权限,而所有文件夹是 755 权限。
# 更改文件夹权限
find . -type d -exec chmod 755 {} \;
# 更改普通文件权限
find . -type f -exec chmod 644 {} \;
第三步:重启nginx即可
问题2:
描述:在重启nginx的时候报错
nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)
解决方案:
第一步:
nginx -t //找到nginx.conf路径
会显示:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
第二步:重新指定位置
nginx -c /etc/nginx/nginx.conf //我的是在这个位置下
如果报错:
nginx: [emerg] bind() to 0.0.0.0:7080 failed (98: Address already in use)
执行第三步:
使用 fuser -k 7080/tcp 杀掉对应进程。之后重启即可成功。
问题3:
描述:nginx部署前端网站后访问报403错误。权限问题导致的报错。
解决方案:
第一步:
更改 nginx.conf 的第一行user xxxx修改为root用户即可。
user root;
-
再次访问发现报500错误。原因:配置文件写错,配置文件如下:
server { listen 7081;#监听端口 server_name localhost;#监听域名 charset utf-8;#指定网页的编码格式 location / { alias /usr/share/nginx/html/musicapp; index index.html; try_files $uri $uri/ /index.html; } }
解决方案: 将location中的alias改为root即可,如下。
server {
listen 7081;#监听端口
server_name localhost;#监听域名
charset utf-8;#指定网页的编码格式
location / {
root /usr/share/nginx/html/musicapp;
index index.html;
try_files $uri $uri/ /index.html;
}
}