阿里云cenos安装nginx

本文详细介绍了Nginx的安装过程,包括必用组件的安装,从官网下载、解压到最终的启动。此外,还讲解了如何配置Nginx以访问外部端口,实现灰度发布,转发IP,以及使用Lua脚本代理Redis集群等高级功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装必用组件

yum -y install gcc-c++
yum -y install pcre pcre-devel
yum -y install zlib zlib-devel
yum -y install openssl openssl–devel

cd /usr/local

官网下载nginx

wget -c https://nginx.org/download/nginx-1.12.1.tar.gz

解压

tar -xvf nginx-1.12.1.tar.gz

安装

cd nginx-1.12.1

./configure

make

make install
启动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命令强制杀掉进程。

ps aux | grep nginx

重启nginx
./nginx -s quit
./nginx

软链接nginx路径
sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin

安装配置可以访问外部端口

手动安装
openssl-1.0.2k.tar.gz

./configure --with-openssl=/data/openssl-1.0.2 --with-http_ssl_module
make
make install

然后就可以访问了

nginx: [error] invalid PID number “” in “/usr/local/nginx/logs/nginx.pid”

nginx -c /usr/local/nginx/conf/nginx.conf

添加stream及mail模块

./configure --with-mail --with-stream

telnet slavenode1 465可用来测试smtp是否连接上

转发smtp请求

stream
{
        server
        {
                listen 9003;
                proxy_pass smtp.exmail.qq.com:25;
        }
}

灰度发布

https://zhuanlan.zhihu.com/p/311539717

ab两套环境,通过redis里的值来判断是否在b环境这样来切换

转发ip,避免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;

openrestry

因为是同步非阻塞的,举个例子比如直连redis集群,请求和响应的过程是同步阻塞的,不能同时承载百万级的并发。通过openrestry做代理中转后,就可以承载了。
openrestry提供了门编程语言的方式,作为一个web服务器来处理逻辑,提高cpu使用率,充分利用服务器资源

设计说明
https://developer.aliyun.com/article/910364

代理redis
修改nginx.conf文件

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
       server {
        listen 8079;

        location / {
            resolver 8.8.4.4;  # use Google's open DNS server

            access_by_lua '
                local key = ngx.var.arg_key
                if not key then
                    ngx.log(ngx.ERR, "no key found")
                    return ngx.exit(400)
                end

                local redis = require "resty.redis"
                local red = redis:new()

                red:set_timeout(1000) -- 1 second

                local ok, err = red:connect("192.168.17.141", 6389)
                if not ok then
                    ngx.log(ngx.ERR, "failed to connect to redis: ", err)
                    return ngx.exit(500)
                end
                red:auth("redis.123")
                local host, err = red:get(key)
                if not host then
                    ngx.log(ngx.ERR, "failed to get redis key: ", err)
                    return ngx.exit(500)
                end

                if host == ngx.null then
                    ngx.log(ngx.ERR, "no host found for key ", key)
                    return ngx.exit(400)
                end
                ngx.say(host)
            ';
        }
    }
}

发起请求
192.168.17.141:8079?key=foo

如果redis里存了key为foo的值,那就会正确返回

lvs+keepalived+openrestry

实现虚拟ip转发

参考文档
https://blog.youkuaiyun.com/lupengfei1009/article/details/86514445
https://www.cnblogs.com/caoweixiong/p/14690527.html

csdn博客拿realserver脚本,其余看博客园的

进入init文件夹
cd /etc/init.d/
编辑脚本
vim realserver

#虚拟的vip 根据自己的实际情况定义
SNS_VIP=192.168.1.200
/etc/rc.d/init.d/functions
case "$1" in
start)
       ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
       /sbin/route add -host $SNS_VIP dev lo:0
       echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
       echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
       sysctl -p >/dev/null 2>&1
       echo "RealServer Start OK"
       ;;
stop)
       ifconfig lo:0 down
       route del $SNS_VIP >/dev/null 2>&1
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
       echo "RealServer Stoped"
       ;;
*)
       echo "Usage: $0 {start|stop}"
       exit 1
esac
exit 0

保存并设置脚本的执行权限

chmod 755 /etc/init.d/realserver
service realserver start
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值