nginx

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > nginx > Keepalived Nginx双机配置

Keepalived+Nginx双机配置小结

作者:ji081

本文主要介绍了Keepalived+Nginx双机配置小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1.1 软硬件要求

1.2 部署前服务器配置调优

关闭SELinux
修改 /etc/selinux/config 文件中的 SELINUX 参数值为 disabled

关闭防火墙

chkconfig iptables off
service iptables stop

修改hostname
修改 /etc/hosts 文件,添加服务器真实 IP 和 hostname。

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
192.168.1.100    OSS-103

修改用户进程可打开文件数限制

修改 /etc/security/limits.conf 文件,添加:

* soft nproc 65536
* hard nproc 65536
* soft nofile 65536
* hard nofile 65536

修改 /etc/security/limits.d/90-nproc.conf 文件,注释掉:

* soft nproc 1024

修改 /etc/pam.d/login 文件,添加:

session required pam_limits.so

重启 SSH 服务:

service sshd restart

Linux服务器时间同步

1.3 Nginx+Keepalived部署

1.3.1 Nginx部署

安装约定

安装过程

Nginx配置

查看 nginx 安装目录:

cd /home/nginx
ls

修改 nginx 配置文件 nginx.conf

vi conf/nginx.conf

配置内容(示例):

worker_processes  24;
events {
    worker_connections  1024;
}

http {
    include       	mime.types;
    default_type  		application/octet-stream;
    sendfile        		on;
    keepalive_timeout 	65;

    upstream xxxx{
        ip_hash;
        server 192.168.1.101:8088 weight=1;
    }

    server {
        listen       8099;
        server_name  localhost;
        location / {
            root   html;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 600;
            proxy_read_timeout 600;
            proxy_send_timeout 600;
            proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; 
            proxy_pass http://xxxx/;
        }
    }

    upstream csss {
        ip_hash;
        server 192.168.1.102:8080 weight=1;
    }

    server {
        listen       9000;
        server_name  192.168.1.102;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://xxxx/;
        }
    }
}

1.3.2 Nginx+Keepalived实现双机热备

安装Keepalived

1.3.3 启动和停止Nginx

启动 Nginx:

/home/nginx/sbin/nginx

停止 Nginx:

从容停止:

kill -QUIT `cat /home/nginx/logs/nginx.pid`

快速停止:

kill -TERM `cat /home/nginx/logs/nginx.pid`

强制停止:

kill -9 `cat /home/nginx/logs/nginx.pid`

平滑重启:

kill -HUP `cat /home/nginx/logs/nginx.pid`

1.4 使用和维护

1.5 在已经安装的Nginx上增加SSL模块

1.6 配置示例

SSL 配置

server {
    server_name example.com;
    listen 443 ssl;
    ssl_certificate /usr/local/nginx/conf/example.com_server.txt;
    ssl_certificate_key /usr/local/nginx/conf/example.com_private.txt;

    location / {
        # 配置内容
    }

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

server {
    listen 80;
    server_name example.com;
    rewrite ^(.*)$ https://example.com$1 permanent;
}

以上是对 Keepalived+Nginx 双机配置的详细指南,包括安装、配置、启动、停止和维护等步骤

1.6 Nginx基线配置

1.6.1 检查是否隐藏nginx版本信息

server_tokens off;

1.6.2 检查是否配置日志

修改 nginx.conf 文件

error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

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  logs/access.log  main;
error_log  logs/error.log  error;

1.6.3 检查是否控制超时时间

修改 nginx.conf 文件

client_body_timeout 20s; # 设置客户端请求主体读取超时时间
client_header_timeout 10s; # 设置客户端请求头读取超时时间
send_timeout 30s; # 服务端向客户端传输数据的超时时间

1.6.4 检查是否限制客户端下载速度

修改 nginx.conf 文件

limit_conn_zone $binary_remote_addr zone=addr:10m;   # 添加该行
limit_conn addr 50; # 每个客户端允许50个线程。
limit_rate 1000k;    # 每个线程最大下载速度1000k

1.6.5 检查是否自定义nginx返回的错误信息

修改 nginx 配置文件

error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 504 506 /50x.html;

location = /50x.html {
    root   html;
}

1.6.6 检查是否配置防盗链设置

location ~* ^.+\.(aa|bb|cc)$ {                   
    valid_referers none blocked 127.0.0.1;    
    if ($invalid_referer) {
        return 403; 
    }
}

1.6.7 检查是否限制IP访问

deny 1.1.1.1; 
allow all;

1.7 完整样例配置

worker_processes  4;

error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

events {
    worker_connections  1024;
}

http {
    include       	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"';
    access_log  logs/access.log  main;
    error_log  logs/error.log  error;
    client_body_timeout 20s;
    client_header_timeout 10s;
    send_timeout 30s;
    limit_conn_zone $binary_remote_addr zone=addr:10m;
    sendfile        		on;
    keepalive_timeout 	65;
    server_tokens off;

    upstream xxxx {
        ip_hash;
        server 192.168.107.2:7001 weight=10;
        server 192.168.107.3:7001 weight=10;
    }

    server {
       listen 8080 default;
       server_name _;
       location / {
            return 403;
          }
    }

    server {
        listen       8080;
        server_name  192.168.107.2 192.168.107.4;
        add_header Set-Cookie "HttpOnly=true";
        set $flag 0;
        if ( $host != '192.168.107.2' ) {
          set $flag 1;  
        }
        if ( $host != '192.168.107.4' ) {
          set $flag $flag+1; 
        }
        if ( $flag = 3 ) {
          return 403; 
        }

        location / {
            root   html;
	    proxy_set_header Host $host:$server_port;
 	    proxy_set_header X-Real-IP $remote_addr;
	    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	    proxy_connect_timeout 600;
	    proxy_read_timeout 600;
	    proxy_send_timeout 600;
	    proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; 
	    proxy_pass http://xxxx/;
	    limit_conn addr 50;
            limit_rate 1000k;
	   deny 1.1.1.1;
	    allow all;
        }
        location ~* ^.+\.(aa|bb|cc)$ {                   
            valid_referers none blocked 127.0.0.1;    
            if ($invalid_referer) {
                return 403; 
            }
         }
        error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 504 506 /50x.html;

        location = /50x.html {
            root   html;
        }
    }
}

以上是 Nginx 的基线配置指南,包括隐藏版本信息、配置日志、控制超时时间、限制客户端下载速度、自定义错误信息、配置防盗链设置和限制 IP 访问等。

到此这篇关于Keepalived+Nginx双机配置小结的文章就介绍到这了,更多相关Keepalived Nginx双机配置内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文