centos7部署nginx与vue搭配及403排错
*以下都是在centos7系统下进行
一.安装
- 添加yum源
sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- 安装
sudo yum install nginx
- 配置服务
- 设置开机启动
sudo systemctl enable nginx
- 启动服务
sudo systemctl start nginx
- 重启服务
sudo systemctl restart nginx
- 停止服务
sudo systemctl stop nginx
- 重新加载,因为一般重新配置之后,不希望重启服务,这时可以使用重新加载。
sudo systemctl reload nginx
- 设置开机启动
二.config配置
自动安装的nginx配置文件一般都在 /etc/nginx下面。
我们需要编辑 /etc/nginx/conf.d下面的default.conf文件。一般修改如下
server {listen80; # 监听端口server_name localhost; # 监听服务器#charset koi8-r;#access_log /var/log/nginx/host.access.log main;location / {root/root/monkey-help/UIServer/dist; # vue的文件所在目录,主要为distindex index.html index.htm; # dist下面的index}以下可以不用管#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page500 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_passhttp://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#roothtml;#fastcgi_pass127.0.0.1:9000;#fastcgi_index index.php;#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;#includefastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#deny all;#}}
三.问题总结
- 服务器策略组需要配置端口允许出入
- 服务器内部防火墙需要打开端口
- 如遇到403,有四种情况,解决方法如下:
- 1、查看主进程与功能进程是否为同一用户启动。
ps aux | grep "nginx: worker process" | awk '{print $1}'
如果不是同一用户需要修 /etc/nginx/nginx.conf 文件。修改如下:user root; # 主要修改这个worker_processes 1;error_log /var/log/nginx/error.log wa;pid/var/run/nginx.pid;events {worker_connections 1024;}http {include/etc/nginx/mime.types;default_type application/octet-stream;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;sendfileon;#tcp_nopush on;keepalive_timeout 65;#gzip on;include /etc/nginx/conf.d/*.conf; }
- 2、缺少index.html或者index.php文件,就是配置文件中index index.html index.htm这行中的指定的文件
- 3、权限问题,如果nginx没有web目录的操作权限,也会出现403错误。解决办法:修改web目录的读写权限,或者是把nginx的启动用户改成目录的所属用户,重启Nginx即可解决
sudo chmod -R 777 目录path
修改目录权限
sudo chown -R 用户名 目录path
修改目录所属用户
sudo chgrp -R 用户组 目录path
修改目录所属用户组 - 4、SELinux设置为开启状态(enabled)的原因
/usr/sbin/sestatus
查看SELinux状态
将SELINUX=enforcing 修改为 SELINUX=disabled 状态
临时修改(重启失效):
sudo setenforce 0
修改配置文件(永久修改,重启生效):
vi /etc/selinux/config
#SELINUX=enforcing
SELINUX=disabled
将SELINUX=enforcing 修改为 SELINUX=disabled 状态
- 1、查看主进程与功能进程是否为同一用户启动。
作者:桜満三葉
来源链接:https://www.cnblogs.com/zy-mousai/p/14158457.html
版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。
2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。