centos7 编译安装nginx+tcp转发
一、依赖
1. gcc 安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
yum install gcc-c++
2. PCRE pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:
yum install -y pcre pcre-devel
3. zlib 安装
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install -y zlib zlib-devel
4. OpenSSL 安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
yum install -y openssl openssl-devel
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
二、安装包下载
直接下载.tar.gz
安装包,地址:https://nginx.org/en/download.html
wget -c https://nginx.org/download/nginx-1.14.0.tar.gz
三、解压
tar zxvf nginx-1.14.0.tar.gz
四、配置安装
cd nginx-1.14.0
./configure --prefix=/usr/local/nginx --with-stream
make && make install
mkdir -p /var/cache/nginx /etc/nginx/http.d /etc/nginx/stream.d
五、配置nginx转发
stream {
upstream proxy_card {
# simple round-robin 转发IP和端口
server 192.168.1.12:;
server 192.168.1.13:;
#check interval= rise= fall= timeout=;
#check interval= rise= fall=5timeout=
#check interval= rise= fall=5timeout=
#check_http_send "GET /HTTP/1.0\r\n\r\n";
#check_http_expect_alive http_2xxhttp_3xx;
}
server {
listen ; #监听端口
proxy_pass proxy_card; #转发请求
}
}
六、工具
查找安装路径:
whereis nginx
启动停止
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
./nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
查询nginx进程:
ps aux|grep nginx
重启
.先停止再启动(推荐):
对 nginx 进行重启相当于先停止再启动,即先执行停止命令再执行启动命令。如下: ./nginx -s quit
./nginx
.重新加载配置文件:
当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 ngin x再启动 nginx 即可将配置信息在 nginx 中生效,如下:
./nginx -s reload
自启动:
即在rc.local增加启动代码就可以了。 vi /etc/rc.local
增加一行 /usr/local/nginx/sbin/nginx
设置执行权限: chmod rc.local
七、tcp转发测试
编辑/etc/nginx/nginx.conf文件,追加
stream {
log_format proxy '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time "$upstream_addr" '
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
access_log /var/log/nginx/stream.access.log proxy; upstream slb_test_apiserver_local {
server 172.31.185.139: weight= max_fails= fail_timeout=30s;
server 172.31.185.138: weight= max_fails= fail_timeout=30s;
server 172.31.185.137: weight= max_fails= fail_timeout=30s;
} server {
listen ;
proxy_pass slb_test_apiserver_local;
proxy_connect_timeout 1s;
proxy_timeout 3s;
access_log /var/log/nginx/slb_test_apiserver_local.log proxy;
}
}
重启nginx,测试成功
八、快速编译配置
快速编译可以应付大部分情况
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/tmp/echo-nginx-module
其中,如不需要echo模块,将最后一个选项去掉,如需要echo模块,从github上面拉到指定位置
九、grpc协议转发
upstream myservice {
server 10.0.0.1: weight= max_fails= fail_timeout=30s;
server 10.0.0.2: weight= max_fails= fail_timeout=30s;
}
server {
listen http2 ;
server_name www.myhost.com;
if ( $host != "www.myhost.com"){
return ;
}
location / {
grpc_pass grpc://myservice;
proxy_connect_timeout 1s;
}
access_log /var/log/nginx/myservice.log main;
}
十、nginx.conf配置文件如下
user nobody; #Single core
worker_processes ; #Multicore
#worker_processes ;
#worker_cpu_affinity ; error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid; events {
worker_connections ;
} http {
include /etc/nginx/mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$http_host"'
'$request_time $upstream_response_time $pipe - $upstream_addr'; log_format post_format $request_body; access_log /var/log/nginx/access.log main; sendfile on;
#tcp_nopush on; keepalive_timeout ;
proxy_connect_timeout ;
proxy_send_timeout 600s;
proxy_read_timeout 600s; #gzip on; include /etc/nginx/http.d/*.conf;
} stream {
log_format proxy '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time "$upstream_addr" '
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
access_log /var/log/nginx/stream.access.log proxy; include /etc/nginx/stream.d/*.conf;
}
centos7 编译安装nginx+tcp转发的更多相关文章
- Centos7 编译安装 Nginx PHP Mariadb Memcached 扩展 ZendOpcache扩展 (实测 笔记 Centos 7.3 + Mariadb 10.1.20 + Nginx 1.10.2 + PHP 7.1.0 + Laravel 5.3 )
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7-x86_64-Minimal-1611.iso 安装步骤: 1.准备 1.0 查看硬 ...
- CentOS7 编译安装 Nginx (实测 笔记 Centos 7.0 + nginx 1.6.2)
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7.0-1406-x86_64-DVD.iso 安装步骤: 1.准备 1.1 显示系统版 ...
- Centos7 编译安装 Nginx Mariadb Asp.net Core2 (实测 笔记 Centos 7.3 + Openssl 1.1.0h + Mariadb 10.3.7 + Nginx 1.14.0 + Asp.net. Core 2 )
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7-x86_64-Minimal-1611.iso 安装步骤: 1.准备 1.0 查看硬 ...
- Centos7 编译安装 Nginx PHP Mariadb Memcached 扩展 ZendOpcache扩展 (实测 笔记 Centos 7.3 + Openssl 1.1.0e + Mariadb 10.1.22 + Nginx 1.12.0 + PHP 7.1.4 + Laravel 5.4 )
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7-x86_64-Minimal-1611.iso 安装步骤: 1.准备 1.0 查看硬 ...
- Centos7 编译安装 Nginx PHP Mariadb Memcache扩展 ZendOpcache扩展 (实测 笔记 Centos 7.0 + Mariadb 10.1.9 + Nginx 1.9.9 + PHP 5.5.30)
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7-x86_64-Minimal-1503-01.iso 安装步骤: 1.准备 1.1 ...
- Centos7 编译安装 Nginx、MariaDB、PHP
前言 本文主要大致介绍CentOS 7下编译安装Nginx.MariaDB.PHP.面向有Linux基础且爱好钻研的朋友.技艺不精,疏漏再所难免,还望指正. 环境简介: 系统: CentOS 7,最小 ...
- Centos7编译安装Nginx+keepalived
一.安装环境.主机信息及软件版本 Nginx:1.12.2keepalived:2.0.12时间同步(同步后确认各服务器时间是否一致,不一致需要修改一下时区) 关闭防火墙 二.编译安装Nginx 1. ...
- centos7编译安装nginx及无缝升级https
安装依赖: yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel 下载nginx: wget -c ...
- Centos7 编译安装Nginx 教程
相信经过上篇博文的学习,聪明的你已经学会了如何在Centos7 上通过yum 方式安装Nginx ,但是有时候有些场景或者特俗情况下,我们往往需要通过编译源码方式安装,以便于更灵活地定制我们的Ngin ...
随机推荐
- Filedset
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- PowerDesiger 生成C#实体类,字段转变成大小写方法
1.打开编辑生成C#实体类的PD脚本 2..L: 表示全部转小写,.U: 表示全部转成大写.
- Alsa aplay S8 U8 S16_LE S16_BE U16_LE U16_BE格式
举个例子 aplay -r 16000 -f S16_LE -D hw:0,0 -c 2 -d 3 ~/Private/Private_Tools/02_ALSA_Learning/left_1k_r ...
- k8s学习笔记之三:k8s快速入门
一.前言 kubectl是apiserver的客户端工具,工作在命令行下,能够连接apiserver上实现各种增删改查等各种操作 kubectl官方使用文档:https://kubernetes.io ...
- sqlzoo 之 nobel 错题集
12.查找尤金•奧尼爾EUGENE O'NEILL得獎的所有細節 我: select yr , subject , winner from nobel where winner = 'eugene o ...
- spring 大会的启示
1.事件驱动的微服务编程 2.无服务架构的编程模型 3.微服务缓存
- GIL计算python 2 和 python 3 计算密集型
首先我画了一张图来表示GIL运行的方式: Python 3执行如下计算代码:#-*-conding:utf-8-*-import threading import timedef add(): n = ...
- java 流转换BASE64的一些问题
java 转换BASE64过程中,出现很多结尾为空的问题!暂时不清楚为什么会这样- ``` java //根据url地址转换成BASE64 public static String getURLIma ...
- ReactiveX 学习笔记(23)RxCpp
RxCpp RxCpp 是 ReactiveX 的 C++ 语言实现. 下载 RxCpp $ git clone --recursive https://github.com/ReactiveX/Rx ...
- Thinkphp语句拼接
例如查询Stu表中年龄大于18,或者身高低于180cm的男性(1为男性),(例子不太好标题有可能不符,望见谅) $where['age'] = array("gt",18); $w ...