CentOS7通过YUM安装Nginx 或 Docker 安装 Nginx

CentOS7通过YUM安装Nginx

1.将nginx放到yum repro库中

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2.查看nginx信息

yum info nginx

在这里插入图片描述

3.使用yum安装ngnix

yum -y install nginx

4.启动服务

systemctl start nginx
# 重启
systemctl restart nginx
# 停止
systemctl stop nginx
# 查看状态
systemctl status nginx

5.设置开机启动

systemctl enable nginx

详细配置代理
nginx 安装位置:/etc/nginx
可在 /etc/nginx/conf.d 里面配置各种反向代理的配置文件
重点:配置好文件后必须刷新nginx配置,再重启nginx
由于安装会默认把nginx配置到系统环境,可在任何目录直接使用nginx -t 进行刷新

# 重载配置
nginx -t  
# 重启
nginx -s reload

在重启nginx时有可能会遇到一下问题,那就是我们的nginx还未启动,这是需要我们先运行启动服务命令 第4步

[root@yixyz web]# nginx -s reload
nginx: [error] invalid PID number "" in "/run/nginx.pid"

6.卸载nginx

# 停止nginx
systemctl stop nginx
# 查找nginx
find / -name mysql
# 删除Nginx的自动启动
chkconfig nginx off
# 删除nginx文件
rm -rf /usr/sbin/nginx
rm -rf /etc/nginx
rm -rf /etc/init.d/nginx
# 清除yum源
yum remove nginx

Docker 安装 Nginx

1.拉取镜像

# 查看镜像
docker search nginx
# 拉取镜像
docker pull nginx

2.创建挂载目录

# 创建挂载目录
mkdir -p /data/nginx/{conf,conf.d,cert.d,html,logs}

目录详解:
conf:放nginx.conf 主要配置文件
conf.d:放一些其他的子配置文件
html:放静态页面
logs:nginx的日志文件

3.在conf目录下创建nginx.conf

cd /data/nginx/conf
vim nginx.conf

将下列配置放到 nginx.conf 中,下面是默认的配置:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
		location / {
		# 统一以POST方式转发请求到服务端
		# proxy-method=POST;
		# 转发请求头里的信息
        # proxy_set_header Host $host;
        # 转发真实的IP
        # proxy_set_header X-Real-IP $remote_addr; 
        # 转发请求的IP信息列表
        # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        # proxy_set_header X-Forwarded-Proto $scheme;
        # proxy_set_header X-Forwarded-Port $server_port;
    	if (!-e $request_filename){
            rewrite ^(.*)$ /usr/share/nginx/html/ last; break;
        }
    }
        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

server 块:
server address [params]
params参数说明:
1.down表示单前的server暂时不参与负载
2.weight为weight越大,负载的权重就越大。
3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

4.创建容器

docker run -d --restart=always --name nginx  -p 80:80 -p 443:443  -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/conf.d:/etc/nginx/conf.d -v /data/nginx/cert.d:/etc/nginx/cert.d -v /data/nginx/logs:/var/log/nginx  nginx

参数说明:
–name:容器名
-p:映射宿主主机端口
-v:挂载宿主目录/容器目录
-d:后台运行容器
–restart=always: docker启动容器自动启动

容器创建成功后通过 IP:8080 就能访问了。
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LOVE_DDZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值