Centos7 Nginx安装步骤及常见问题汇总,亲测有效!

  • CentOS 7 Redis安装、数据目录更改新位置,亲测有效!
  • CentOS 7 MariaDB安装、卸载、数据目录更改为新位置

1、安装Nginx

>  sudo yum install nginx
  • 如果没有安装源,按照版本执行源添加

//centos6
rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
//centos7
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2、设置开机启动服务

> sudo systemctl enable nginx

3、启动/停止/重启Nginx服务

  • 启动
> sudo systemctl start nginx
  • 停止
> sudo systemctl stop nginx
  • 重启
> sudo systemctl restart nginx

4、查看运行状态

> sudo systemctl status nginx

5、测试访问

>  curl 127.0.0.1



  Welcome to CentOS
  







Welcome to CentOS

The Community ENTerprise Operating System

CentOS is an Enterprise-class Linux Distribution derived from sources freely provided to the public by Red Hat, Inc. for Red Hat Enterprise Linux. CentOS conforms fully with the upstream vendors redistribution policy and aims to be functionally compatible. (CentOS mainly changes packages to remove upstream vendor branding and artwork.)

CentOS is developed by a small but growing team of core developers.  In turn the core developers are supported by an active user community including system administrators, network administrators, enterprise users, managers, core Linux contributors and Linux enthusiasts from around the world.

CentOS has numerous advantages including: an active and growing user community, quickly rebuilt, tested, and QA'ed errata packages, an extensive mirror network, developers who are contactable and responsive, Special Interest Groups (SIGs) to add functionality to the core CentOS distribution, and multiple community support avenues including a wiki, IRC Chat, Email Lists, Forums, Bugs Database, and an FAQ.

  • 就此完成安装

6、前后端分离配置


7、常见错误

  • # (13: Permission denied) while connecting to upstream

2019/11/06 10:53:49 [error] 79727#0: *2 open() "/home/xnzf/www/index.html" failed (13: Permission denied), client: 202.0.82.97, server: , request: "GET /index.html HTTP/1.1", host: "202.0.81.70:7010"

2019/11/06 10:53:07 [error] 79455#0: *1 open() "/home/xnzf/www/index.html" failed (13: Permission denied), client: 202.0.82.97, server: , request: "GET /index.html HTTP/1.1", host: "202.0.81.70:7010"

原因及解决方案:问题是由SElinux导致,查看audit日志:

> sudo cat /var/log/audit/audit.log | grep nginx | grep denied

错误信息:

type=AVC msg=audit(1573009833.405:27595): avc: denied { name_connect } for pid=83616 comm="nginx" dest=8011 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:unreserved_port_t:s0 tclass=tcp_socket

修复此错误:

sudo cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
******************** IMPORTANT ***********************
To make this policy package active, execute:

semodule -i mynginx.pp

执行修复

sudo semodule -i mynginx.pp

问题最终得到解决。

  • 安装nginx提示
[xnzf@localhost ~]$ sudo yum install nginx
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.cn99.com
 * updates: mirrors.163.com
没有可用软件包 nginx。
错误:无须任何处理

解决办法:阿里yum源加速
安装base reop源
备份旧源

cd /etc/yum.repos.d
sudo mv CentOS-Base.repo CentOS-Base.repo.bak

修改为阿里源

sudo wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

安装epel repo源
epel(RHEL 7)

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

epel(RHEL 6)

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

epel(RHEL 5)

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-5.repo
  • 配置 EPEL源
sudo yum install -y epel-release
sudo yum -y update
  • 安装Nginx
sudo yum install -y nginx

刷新缓存

yum clean all
yum makecache
  • nginx 代理转发时,此页面不能正确地重定向,连接到 www.website.com 时发生错误。
        location / {
          proxy_pass         http://app.website.com/;
          proxy_set_header   Host            $host;
          proxy_set_header   Remote_Addr     $remote_addr;
          proxy_set_header   X-Real-IP       $remote_addr;
          proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        }

去除proxy就可以了

        location / {
          proxy_pass         http://app.website.com/;
        }

你可能感兴趣的:(Centos7 Nginx安装步骤及常见问题汇总,亲测有效!)