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 {
    listen       80; # 监听端口
    server_name  localhost; # 监听服务器
    
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /root/monkey-help/UIServer/dist; # vue的文件所在目录,主要为dist
        index  index.html index.htm; # dist下面的index
    }
    以下可以不用管
    #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;
    #}
}

三.问题总结

  • 服务器策略组需要配置端口允许出入
  • 服务器内部防火墙需要打开端口
  • 如遇到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 warn;
      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;
          
          sendfile        on;
          #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 状态