centos8安装nginx

下载

  1. 需要安装gcc环境 nginx有好多c++库文件
yum install gcc-c++
  1. PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。
yum install -y pcre pcre-devel
  1. nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
install -y zlib zlib-devel
  1. nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。 经过poenssl包装nginx后nginx可以调用大量库文件,还可以运行lua脚本
install -y openssl openssl-devel

上面的有错误执行下面的

yum -y install openssl openssl-devel make zlib zlib-devel gcc gcc-c++ libtool    pcre pcre-devel

  1. 下载nginx 到linux
cd /
mkdir download
cd /download/
wget https://nginx.org/download/nginx-1.21.5.tar.gz

安装

  1. 解压
tar -xf nginx-1.21.5.tar.gz
  1. 执行解压文件
cd nginx-1.21.5

解释:
–prefix=/nginx-1.21.5
设置安装目录:安装目录: /nginx-1.21.5
8. 复制下面的命令

./configure \
--prefix=/nginx-1.21.5 \
--pid-path=/nginx-1.21.5/temp/run/nginx/nginx.pid \
--lock-path=/nginx-1.21.5/temp/lock/nginx.lock \
--error-log-path=/nginx-1.21.5/temp/log/nginx/error.log \
--http-log-path=/nginx-1.21.5/temp/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/nginx-1.21.5/temp/temp/nginx/client \
--http-proxy-temp-path=/nginx-1.21.5/temp/temp/nginx/proxy \
--http-fastcgi-temp-path=/nginx-1.21.5/temp/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/nginx-1.21.5/temp/temp/nginx/uwsgi \
--http-scgi-temp-path=/nginx-1.21.5/temp/temp/nginx/scgi \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-file-aio \
--with-http_realip_module
  1. 建立临时目录
mkdir /nginx-1.21.5/temp/temp/nginx -p
  1. 编译
make
  1. 安装
make install
  1. 启动nginx
cd /nginx-1.21.5/sbin/
./nginx
  1. 验证是否启动
ps -aux | grep nginx

如果nginx启动

root        4783  0.0  0.0  42348   804 ?        Ss   03:29   0:00 nginx: master process ./nginx
nobody      4784  0.0  0.2  74568  5036 ?        S    03:29   0:00 nginx: worker process
root        4786  0.0  0.0  12348  1176 pts/0    S+   03:29   0:00 grep --color=auto nginx

否则

root        4786  0.0  0.0  12348  1176 pts/0    S+   03:29   0:00 grep --color=auto nginx
  1. 开启防火墙80端口
firewall-cmd --add-port=80/tcp
  1. 网页验证 使用这个命令可以查看linux的ip地址 从浏览器中访问ip地址查看是否有页面出现
ifconfig
  1. 关闭nginx
./nginx -s stop

配置

刷新配置文件

./nginx -s reload

配置文件介绍

第一部分:全局块
从配置文件开始到 events 块之间的内容

# 全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log /nginx-1.21.5/temp/log/nginx/error.log info;

# nginx进程数,建议设置为等于CPU总核心数
worker_processes 8;

第二部分
events
第三部分
http
一. 全局快
二. location

一. 全局快

include mime.types; #nginx支持的媒体类型库文件
default_type application/octet-stream; #默认媒体文件类型
#charset utf-8; #默认编码
server_names_hash_bucket_size 128; #服务器名字的hash表大小
client_header_buffer_size 32k; #上传文件大小限制
sendfile on; #开启高效文件传输模式,实现内核零拷贝
autoindex off; #开启目录列表访问,适合下载服务器,默认关闭。
keepalive_timeout 120; #长连接超时时间,单位是秒

二. location

server #网站配置区域
{
   listen 80;     #默认监听80端口
    server_name www.zxd.com;    #提供服务的域名主机名 #亲测,无用
    location / { 
            root html;   
            #站点根目录(这里html是相对路径,默认网站根目录为:/usr/local/nginx/html)
            index index.thml index.htm;  #默认首页文件,多个用空格分开
    }
    error_page 500 502 503 504  /50x.html;    #出现对应http状态码时,使用50x.html回应客户
    location = /50x.thml {
    root    html;     #指定对应目录
    }
} 

location中url和转发规则:

location url {
	规则   
}

url
url

符号(优先级递减)作用
=完全匹配
无符号完整路径完全匹配
^~先使用等于号在使用正则式
~正则
~*正则
/全部路径都可以

规则

反向代理
转发路径:0 localhost:80 -> 1 192.168.187.136:80 -> 1 index.html

http{
	upstream pass_2{
		server 192.68.187.136:80;
	}
	upstream pass_1{
		server 192.68.187.136:81;
	}
	upstream pass_3{
		server 192.68.187.136:82;
	}
	server{
			listen 80;
			server_name localhost;
			location / {
				proxy_pass pass_2;
				index index.html index.htm
			}
			location / {
				proxy_pass pass_1;
				index index.html index.htm
			}
			location / {
				proxy_pass pass_3;
				index index.html index.htm
			}
	}
}

负载均衡

http{
	upstream pass_1{
		server 192.68.187.136:80;
		server 192.68.187.136:81 weight=2;
		server 192.68.187.136:82;
	}
	server{
			listen 80;
			server_name localhost;
			location / {
				proxy_pass pass_1;
				index index.html index.htm
			}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值