当前位置:首页 > 技术分析 > 正文内容

Nginx 从安装到高可用入门教程

Nginx安装

1、去官网http://nginx.org/下载对应的nginx包,推荐使用稳定版本

2、上传nginx到linux系统

3、安装依赖环境

(1)安装gcc环境

yum install gcc-c++

Nginx 从安装到高可用,入门教程

原创2022-03-09 14:20·司空玄

Nginx安装

1、去官网http://nginx.org/下载对应的nginx包,推荐使用稳定版本

2、上传nginx到linux系统

3、安装依赖环境

(1)安装gcc环境

yum install gcc-c++

(2)安装PCRE库,用于解析正则表达式

yum install -y pcre pcre-devel

(3)zlib压缩和解压缩依赖

yum install -y zlib zlib-devel

(4)SSL 安全的加密的套接字协议层,用于HTTP安全传输,也就是https

yum install -y openssl openssl-devel

4、解压,需要注意,解压后得到的是源码,源码需要编译后才能安装

tar -zxvf nginx-1.16.1.tar.gz

5、编译之前,先创建nginx临时目录,如果不创建,在启动nginx的过程中会报错

mkdir /var/temp/nginx -p

6、在nginx目录,输入如下命令进行配置,目的是为了创建makefile文件

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注:代表在命令行中换行,用于提高可读性配置命令:

7、make编译&安装

make
make install

8、进入sbin目录启动nginx

启动:nginx停止:./nginx -s stop重新加载:./nginx -s reload

Nginx 从安装到高可用,入门教程

原创2022-03-09 14:20·司空玄

Nginx安装

1、去官网http://nginx.org/下载对应的nginx包,推荐使用稳定版本

2、上传nginx到linux系统

3、安装依赖环境

(1)安装gcc环境

yum install gcc-c++

(2)安装PCRE库,用于解析正则表达式

yum install -y pcre pcre-devel

(3)zlib压缩和解压缩依赖

yum install -y zlib zlib-devel

(4)SSL 安全的加密的套接字协议层,用于HTTP安全传输,也就是https

yum install -y openssl openssl-devel

4、解压,需要注意,解压后得到的是源码,源码需要编译后才能安装

tar -zxvf nginx-1.16.1.tar.gz

5、编译之前,先创建nginx临时目录,如果不创建,在启动nginx的过程中会报错

mkdir /var/temp/nginx -p

6、在nginx目录,输入如下命令进行配置,目的是为了创建makefile文件

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注:代表在命令行中换行,用于提高可读性配置命令:

7、make编译&安装

make
make install

8、进入sbin目录启动nginx

启动:nginx停止:./nginx -s stop重新加载:./nginx -s reload


二、配置反向代理

1、配置upstream

upstream [proxyName] {
server 192.168.1.173:8080;
server 192.168.1.174:8080;
server 192.168.1.175:8080;
}

2、配置server

server {
listem 80;
server_name www.tomcats.com;
location / {
proxy_pass http://tomcats;
}
}

Nginx 从安装到高可用,入门教程

原创2022-03-09 14:20·司空玄

Nginx安装

1、去官网http://nginx.org/下载对应的nginx包,推荐使用稳定版本

2、上传nginx到linux系统

3、安装依赖环境

(1)安装gcc环境

yum install gcc-c++

(2)安装PCRE库,用于解析正则表达式

yum install -y pcre pcre-devel

(3)zlib压缩和解压缩依赖

yum install -y zlib zlib-devel

(4)SSL 安全的加密的套接字协议层,用于HTTP安全传输,也就是https

yum install -y openssl openssl-devel

4、解压,需要注意,解压后得到的是源码,源码需要编译后才能安装

tar -zxvf nginx-1.16.1.tar.gz

5、编译之前,先创建nginx临时目录,如果不创建,在启动nginx的过程中会报错

mkdir /var/temp/nginx -p

6、在nginx目录,输入如下命令进行配置,目的是为了创建makefile文件

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注:代表在命令行中换行,用于提高可读性配置命令:

7、make编译&安装

make
make install

8、进入sbin目录启动nginx

启动:nginx停止:./nginx -s stop重新加载:./nginx -s reload


二、配置反向代理

1、配置upstream

upstream [proxyName] {
server 192.168.1.173:8080;
server 192.168.1.174:8080;
server 192.168.1.175:8080;
}

2、配置server

server {
listem 80;
server_name www.tomcats.com;
location / {
proxy_pass http://tomcats;
}
}


三、配置负载均衡

nginx默认采用轮循的方式进行负载均衡

1、使用加权轮询

upstream [proxyName] {
server 192.168.1.173:8080 weight=1;
server 192.168.1.174:8080 weight=5;
server 192.168.1.175:8080 weight=2;
}

2、hash负载均衡

upstream [proxyName] {
ip_hash
server 192.168.1.173:8080;
server 192.168.1.174:8080;
server 192.168.1.175:8080;
}

hash算法实际上只会计算 192.168.1这段做哈希

使用ip_hash的注意点:

  • 不能把后台服务器直接移除,只能标记down.

3、url hash负载均衡

upstream [proxyName] {
hash $request_url;
server 192.168.1.173:8080;
server 192.168.1.174:8080;
server 192.168.1.175:8080;
}

4、最小连接负载均衡

upstream [proxyName] {
least_conn;
server 192.168.1.173:8080;
server 192.168.1.174:8080;
server 192.168.1.175:8080;
}

四、upstream指令参数

  • max_conns:限制最大同时连接数 1.11.5之前只能用于商业版
  • slow_start:单位秒,权重在指定时间内从1上升到指定值,不适用与hash负载均衡、随机负载均衡 如果在 upstream 中只有一台 server,则该参数失效(商业版才有)
  • down:禁止访问
  • backup:备用机 只有在其他服务器无法访问的时候才能访问到 不适用与hash负载均衡、随机负载均衡
  • max_fails:表示失败几次,则标记server已宕机,剔出上游服务 默认值1
  • fail_timeout:表示失败的重试时间 默认值10

1、keepalived

upstream [proxyName] {
server 192.168.1.173:8080 weight=1;
server 192.168.1.174:8080 weight=5;
server 192.168.1.175:8080 weight=2;
keepalive 32; #保持的连接数
}
server {
listem 80;
server_name www.tomcats.com;
location / {
proxy_pass http://tomcats;
proxy_http_version 1.1; #连接的协议版本
proxy_set_header Connection ""; 清空连接请求头
}
}

2、控制浏览器缓存

server {
listem 80;
server_name www.tomcats.com;
location / {
proxy_pass http://tomcats;
expires 10s; #浏览器缓存10秒钟
#expires @22h30m #在晚上10点30的时候过期
#expires -1h #缓存在一小时前时效
#expires epoch #不设置缓存
#expires off #缓存关闭,浏览器自己控制缓存
#expires max #最大过期时间
}
}

Nginx 从安装到高可用,入门教程

原创2022-03-09 14:20·司空玄

Nginx安装

1、去官网http://nginx.org/下载对应的nginx包,推荐使用稳定版本

2、上传nginx到linux系统

3、安装依赖环境

(1)安装gcc环境

yum install gcc-c++

(2)安装PCRE库,用于解析正则表达式

yum install -y pcre pcre-devel

(3)zlib压缩和解压缩依赖

yum install -y zlib zlib-devel

(4)SSL 安全的加密的套接字协议层,用于HTTP安全传输,也就是https

yum install -y openssl openssl-devel

4、解压,需要注意,解压后得到的是源码,源码需要编译后才能安装

tar -zxvf nginx-1.16.1.tar.gz

5、编译之前,先创建nginx临时目录,如果不创建,在启动nginx的过程中会报错

mkdir /var/temp/nginx -p

6、在nginx目录,输入如下命令进行配置,目的是为了创建makefile文件

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注:代表在命令行中换行,用于提高可读性配置命令:

7、make编译&安装

make
make install

8、进入sbin目录启动nginx

启动:nginx停止:./nginx -s stop重新加载:./nginx -s reload


二、配置反向代理

1、配置upstream

upstream [proxyName] {
server 192.168.1.173:8080;
server 192.168.1.174:8080;
server 192.168.1.175:8080;
}

2、配置server

server {
listem 80;
server_name www.tomcats.com;
location / {
proxy_pass http://tomcats;
}
}


三、配置负载均衡

nginx默认采用轮循的方式进行负载均衡

1、使用加权轮询

upstream [proxyName] {
server 192.168.1.173:8080 weight=1;
server 192.168.1.174:8080 weight=5;
server 192.168.1.175:8080 weight=2;
}

2、hash负载均衡

upstream [proxyName] {
ip_hash
server 192.168.1.173:8080;
server 192.168.1.174:8080;
server 192.168.1.175:8080;
}

hash算法实际上只会计算 192.168.1这段做哈希

使用ip_hash的注意点:

  • 不能把后台服务器直接移除,只能标记down.


3、url hash负载均衡

upstream [proxyName] {
hash $request_url;
server 192.168.1.173:8080;
server 192.168.1.174:8080;
server 192.168.1.175:8080;
}

4、最小连接负载均衡

upstream [proxyName] {
least_conn;
server 192.168.1.173:8080;
server 192.168.1.174:8080;
server 192.168.1.175:8080;
}

四、upstream指令参数

  • max_conns:限制最大同时连接数 1.11.5之前只能用于商业版
  • slow_start:单位秒,权重在指定时间内从1上升到指定值,不适用与hash负载均衡、随机负载均衡 如果在 upstream 中只有一台 server,则该参数失效(商业版才有)
  • down:禁止访问
  • backup:备用机 只有在其他服务器无法访问的时候才能访问到 不适用与hash负载均衡、随机负载均衡
  • max_fails:表示失败几次,则标记server已宕机,剔出上游服务 默认值1
  • fail_timeout:表示失败的重试时间 默认值10

1、keepalived

upstream [proxyName] {
server 192.168.1.173:8080 weight=1;
server 192.168.1.174:8080 weight=5;
server 192.168.1.175:8080 weight=2;
keepalive 32; #保持的连接数
}
server {
listem 80;
server_name www.tomcats.com;
location / {
proxy_pass http://tomcats;
proxy_http_version 1.1; #连接的协议版本
proxy_set_header Connection ""; 清空连接请求头
}
}

2、控制浏览器缓存

server {
listem 80;
server_name www.tomcats.com;
location / {
proxy_pass http://tomcats;
expires 10s; #浏览器缓存10秒钟
#expires @22h30m #在晚上10点30的时候过期
#expires -1h #缓存在一小时前时效
#expires epoch #不设置缓存
#expires off #缓存关闭,浏览器自己控制缓存
#expires max #最大过期时间
}
}

3、反向代理缓存

upstream [proxyName] {
server 192.168.1.173:8080 weight=1;
server 192.168.1.174:8080 weight=5;
server 192.168.1.175:8080 weight=2;
}
#proxy_cache_path 设置缓存保存的目录的位置
#keys_zone设置共享内以及占用的空间大小
#mas_size 设置缓存最大空间
#inactive 缓存过期时间,错过此时间自动清理
#use_temp_path 关闭零时目录
proxy_cache_path
/usr/local/nginx/upsteam_cache keys_zone=mycache:5m max_size=1g inactive=8h use_temp_path=off;
server {
listem 80;
server_name www.tomcats.com;
#开启并使用缓存
proxy_cache mycache;
#针对200和304响应码的缓存过期时间
proxy_cache_valid 200 304 8h;
location / {
proxy_pass http://tomcats;
}
}

五、配置ssl证书提供https访问

1. 安装SSL模块

要在nginx中配置https,就必须安装ssl模块,也就是: http_ssl_module

进入到nginx的解压目录:

/home/software/nginx-1.16.1

新增ssl模块(原来的那些模块需要保留)

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--with-http_ssl_module

编译和安装

makemake install

2、配置HTTPS

把ssl证书 *.crt 和 私钥 *.key 拷贝到/usr/local/nginx/conf目录中。

新增 server 监听 443 端口:

server {
listen 443;
server_name www.imoocdsp.com;
# 开启ssl
ssl on;
# 配置ssl证书
ssl_certificate
1_www.imoocdsp.com_bundle.crt;
# 配置证书秘钥
ssl_certificate_key 2_www.imoocdsp.com.key;
# ssl会话cache
ssl_session_cache shared:SSL:1m;
# ssl会话超时时间
ssl_session_timeout 5m;
# 配置加密套件,写法遵循 openssl 标准
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers
ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://tomcats/;
index index.html index.htm;
}
}

六、配置ha nginx

1、安装keepalived

(1)下载

https://www.keepalived.org/download.html

(2)解压

tar -zxvf keepalived-2.0.18.tar.gz

(3)使用configure命令配置安装目录与核心配置文件所在位置:

./configure --prefix=/usr/local/keepalived --sysconf=/etc

  • prefix :keepalived安装的位置sysconf:keepalived核心配置文件所在位置,固定位置,改成其他位置则keepalived启动不了,/var/log/messages中会报错
  • sysconf :keepalived核心配置文件所在位置,固定位置,改成其他位置则keepalived启动不了,/var/log/messages中会报错

配置过程中可能会出现警告信息,如下所示:

*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.
# 安装libnl/libnl-3依赖
yum -y install libnl libnl-devel

4)安装keepalived

make && make install

(5)配置文件 在

/etc/keepalived/keepalived.conf

(6)忘记安装配置的目录,则通过如下命令找到:

whereis keepalived

(7)启动keepalived

进入sbin目录

./keepalived

结语

点击关注我,持续会更新一些java 开放过程中常用的技术文章,喜欢的小伙伴可以点颗红心,或者收藏一下,支持一下笔者哦!!!

扫描二维码推送至手机访问。

版权声明:本文由ruisui88发布,如需转载请注明出处。

本文链接:http://www.ruisui88.com/post/4597.html

标签: java nginx
分享给朋友:

“Nginx 从安装到高可用入门教程” 的相关文章

厅监控结算中心加强高速公路联网收费系统运行管理

厅监控结算中心加强高速公路联网收费系统运行管理,一是严格PSAM卡管理要求,加强跟踪各营运公司PSAM卡使用情况,切实做到PSAM卡专人保管,定期核查,做好联网收费系统基础安全工作。二是督促各营运公司加强3G备份链路管理,保障数据应急通道的畅通,确保车道数据正常传输。三是落实标识站建设工作,督促各营...

30 个纯 HTML5 实现的游戏

浏览器和 JavaScript 的功能逐年不断的变强变大。曾几何时,任何类型的游戏都需要Flash。但随着 HTML5 发展,HTML5 + WebGL 游戏式就慢慢占领着这个舞台。以下是30款流行的游戏,它们可以在所有现代浏览器中运行,并且只使用web技术构建。1. HexGL地址:http://...

vue中router常见的三种传参方式

目录:我们在使用vue开发的过程中使用router跳转的时候肯定会遇到传参的情况;一般情况就三种传参是最常见的;那我们就来看看都有那几种传参方式吧!第一种:{ path: '/mall:id', name: 'Mall', component:...

分享15个基于Vue3.0全家桶的优秀开源项目

大家好,我是 Echa。今天来分享 15 个基于 Vue3.0 全家桶的优秀开源项目!1. Vue Admin Bettergithub : https://github.com/chuzhixin/vue-admin-bettervue admin better 对比其他来源 admin 框架有如...

基于 vue3.0 小程序拖拽定制

今天给大家分享一个使用Vue3编写的自由DIY小程序页面。mbDIY 一款基于vue3.x构建的可拖拽定制小程序模板。支持新建页面、自由拖拽模块、复制/移动、自定义模块样式等功能。整个项目分为页面、模块、控件三大部分。模块里面的组件可拖拽至主面板区,编辑后保存即可预览效果。快速安装# 克隆项目 gi...

Alpine.js 如何火起来的!比 React/Vue 如何?

大家好,很高兴又见面了,我是"高级前端?进阶?",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进步,也欢迎大家关注、点赞、收藏、转发!前言前端 JavaScript 框架的创新是这个时代最伟大的技术文化现象之一。Alpine 发音为 /??lpa?n/,中文为阿尔卑斯山、...