linux 快速安装最新稳定版nginx(CentOS8.1 64位)

目录

CentOS8.1 安装完成已具备 yum 指令操作   低版本缺少yum 指令自行安装

启动nginx遇到错误 nginx: [emerg] bind() to 0.0.0.0:7070 failed (13: Permission denied)

配置开机启动

开机nginx服务启动失败及解决方案

添加TCP协议转发


CentOS8.1 安装完成已具备 yum 指令操作   低版本缺少yum 指令自行安装

1、执行如下命令进行yum安装nginx

yum install nginx

安装过程需要输入确认信息,全部y  回车即可  ssl_module 也已具备 SSL证书使用需求

2、安装完成后  查看版本信息

# 查看nginx版本
nginx -v

# 查看编译参数
nginx -V

#查看安装目录文件
rpm -ql nginx

#查看部署目录
whereis nginx

#查看nginx的配置文件的目录
nginx -t	

#查看帮助信息	
nginx -h  			

 

查询得出   /usr/sbin/nginx  为 nginx 程序    /etc/nginx/nginx.conf为 nginx 配置文件

 

启动nginx的命令

#注意/usr/sbin/ 是nginx程序的根目录 这样写就不需要通过cd 命令进入目录,上下键就是轻松完成
#手动启动nginx
/usr/sbin/nginx -c /etc/nginx/nginx.conf 
#或
systemctl start nginx

#重新载入nginx(当配置信息发生修改时)
/usr/sbin/nginx -s reload
#或
systemctl reload nginx

#停止ngix
/usr/sbin/nginx -s quit
#或
systemctl stop nginx

查看nginx是否成功启动

#查看进程命令
ps -ef | grep nginx

开机自动启动NGINX请见以下文章:Linux开机自动启动nginx   此文章可避免遇到以下问题

或者执行以下服务命令

#yum命令安装可以执行以下指令
systemctl enable nginx

 

启动nginx遇到错误 nginx: [emerg] bind() to 0.0.0.0:7070 failed (13: Permission denied)

解决办法:

权限拒绝,经检查发现是开启selinux 导致的。 直接关闭
getenforce   这个命令可以查看当前是否开启了selinux 如果输出 disabled 或 permissive 那就是关闭了
如果输出 enforcing 那就是开启了 selinux

  • 临时关闭selinux

setenforce 0            ##设置SELinux 成为permissive模式
setenforce 1    ##设置SELinux 成为enforcing模式

 

  • 永久关闭selinux 文件

将SELINUX=enforcing改为SELINUX=disabled
重启机器即可
 

配置开机启动

#开机启动nginx服务
systemctl enable nginx.service



#查询服务是否开机启动
systemctl is-enabled servicename.service 
 
#开机运行服务
systemctl enable *.service 
 
 #取消开机运行
systemctl disable *.service
 
#启动服务
systemctl start *.service 
 
#停止服务
systemctl stop *.service 
 
#重启服务
systemctl restart *.service 
 
#重新加载服务配置文件
systemctl reload *.service 
 
#查询服务运行状态
systemctl status *.service
 
 #显示启动失败的服务
systemctl --failed

开机nginx服务启动失败及解决方案

查看nginx 启动服务已生效

 开机nginx服务没有启动

查看失败服务有启动但是失败了 

问题就在于我配置文件 proxy_pass后紧跟域名 nginx启动或者reload的时候,会对proxy_pass后面的域名进行DNS解析,如果解析失败,启动就会失败。

解决方案:

不直接在proxy_pass后写域名,而通过变量的方式配置,如下:

location /oauth{
                client_max_body_size 200m;
                set $target https://aip.baidubce.com/oauth;
                proxy_pass $target;
              }

除了上面的配置之外,还要在server属性的平行域下使用resolver指令,如下

resolver 114.114.114.114;
 server{
        listen 443 ssl;
        server_name test.demo.com;
		resolver 114.114.114.114;
        ……

 

原因解析:

假如proxy_pass后紧跟域名,在nginx启动的时候,会使用/etc/resolv.conf下配置的DNS服务器进行域名解析。采用变量的方式后,nginx启动时不会再去解析变量所对应的域名,但是会在进行代理访问的时候,进行域名解析,此时不会使用/etc/resolv.conf配置的DNS服务器,必须使用reslover指令指定DNS服务地址。

添加TCP协议转发

.1.yum 安装最新版本已带with-stream  其它手动安装的版本可检查是否编译时带with-stream参数

#有with-stream参数,可以代理tcp协议
nginx -V |grep with-stream

 

2.配置nginx的tcp代理

请注意,stream块和http块是两个不同的模块,stream不属于http模块,即不能放到/etc/nginx/conf.d/,stream是通过tcp层转发,而不是http转发。
如配置在http内,启动nginx会报如下错误:

nginx: [emerg] "server" directive is not allowed here

2.1 修改主配置文件,添加stream目录

cd /etc/nginx/
cp -a nginx.conf{,_$(date +%F)}
vim nginx.conf

# 最后追加如下内容
# tcp/ip proxy
include /etc/nginx/tcp.d/*.conf;

2.2 添加tcp转发配置

mkdir tcp.d
cd tcp.d

在新建的 tcp.d 目录下创建 conf 文件新建一个 tcp 配置,例如我转发到IP为127.0.0.1的7666端口

vim openldap.conf

写入

stream{
    upstream tcpdx{
        hash $remote_addr consistent;
        server  127.0.0.1:8090 max_fails=3 fail_timeout=10s;  
        server  127.0.0.1:8089 max_fails=3 fail_timeout=10s;  
   }
    server{
        listen 8090;
        proxy_connect_timeout 20s;
        proxy_timeout 5m;
        proxy_pass tcpdx;
    }
}

"upstream tcpdx":转发的目的地址和端口等设置;其中tcpdx为自定义;
"server":提供转发的服务,即访问localhost:8090,会跳转至代理"tcpdx"指定的转发地址.。

2.3启动nginx服务

启动nginx服务

systemctl start nginx.service

查看是否启动

[root@node1 ~]# systemctl status nginx.service
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2018-09-29 11:34:01 CST; 5h 37min ago
     Docs: http://nginx.org/en/docs/
 Main PID: 26114 (nginx)
   CGroup: /system.slice/nginx.service
           ├─26114 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─26115 nginx: worker process

Sep 29 11:34:01 node1 systemd[1]: Starting nginx - high performance web server...
Sep 29 11:34:01 node1 systemd[1]: Started nginx - high performance web server.
[root@node1 ~]#

 

 

 

 

 

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值