海运的博客

Nginx反向代理做负载均衡及缓存服务器

发布时间:May 2, 2012 // 分类:高可用 // No Comments

安装编译环境及相关组件:

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 \
libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl \
curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap \
openldap-devel nss_ldap openldap-clients openldap-servers unzip

编译安装PCRE、Nginx及缓存清除模块ngx_cache_purge:

/usr/local/src/
wget http://sourceforge.net/projects/pcre/files/pcre/8.30/pcre-8.30.zip
unzip pcre-8.30.zip 
cd pcre-8.30
./configure 
make && make install  
cd ..
wget http://labs.frickle.com/files/ngx_cache_purge-1.2.tar.gz
tar zxvf ngx_cache_purge-1.2.tar.gz 
wget http://nginx.org/download/nginx-1.0.15.tar.gz
tar zxvf nginx-1.0.15.tar.gz 
cd nginx-1.0.15
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module  \
--with-http_gzip_static_module --add-module=../ngx_cache_purge-1.2
make && make install  

Nginx配置文件:

user  www www;

worker_processes 4;

error_log  /usr/local/nginx/logs/nginx_error.log  crit;

pid        /usr/local/nginx/logs/nginx.pid;

worker_rlimit_nofile 65535;

events
    {
        use epoll;
        worker_connections 65535;
    }

http
    {
        include       mime.types;
                default_type application/octet-stream; 

                charset utf-8;   

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile on;
        tcp_nopush     on;

        keepalive_timeout 60;

        tcp_nodelay on;

                proxy_temp_path /cache_tmp;
                proxy_cache_path /cache levels=1:2 keys_zone=proxy_cache:256m inactive=10d max_size=10g;

                client_body_buffer_size  512k;
                proxy_connect_timeout 5;   
                proxy_read_timeout 60;   
                proxy_send_timeout 15; 
                proxy_buffer_size        16k;
                proxy_buffers            4 64k;
                proxy_busy_buffers_size 128k;
                proxy_temp_file_write_size 128k;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types       text/plain application/x-javascript text/css application/xml;
        gzip_vary on;

                upstream proxy_server_pool 
                {
                server node1.www.haiyun.me;
                server node2.www.haiyun.me;
                }
                 

                 log_format   proxy
                 $remote_addr-$remote_user-$time_local-$request-$body_bytes_sent-$http_referer-
                 $http_user_agent-$upstream_addr-$upstream_cache_status-$upstream_status ;
server
        {
                 listen       80;
                 server_name cdn.www.haiyun.me;
                 location /
                 {
                 proxy_next_upstream http_502 http_504 error timeout invalid_header;   
                 proxy_cache proxy_cache;
                 proxy_cache_valid  200 304 1d;
                 proxy_cache_key $host$uri$is_args$args; #$http_cookie
                 add_header Nginx-Cache "$upstream_cache_status from $upstream_addr";
                 proxy_set_header Host $host;
                 proxy_set_header X-Forwarded-For $remote_addr;
                 proxy_pass http://proxy_server_pool;
                 }

                 location ~ .*\.(php|jsp|cgi)?$
                 {
                 proxy_set_header Host $host;
                 proxy_set_header X-Forwarded-For $remote_addr;
                 proxy_pass http://proxy_server_pool;
                 }
                 location ~ /purge(/.*)   
                 {    
                 allow 127.0.0.1;   
                 deny all;   
                 proxy_cache_purge proxy_cache $host$uri$is_args$args;   
                 }   
                 access_log  /usr/local/nginx/logs/cdn.www.haiyun.me.log proxy;
         }
}

Putty小工具Plink妙用

发布时间:May 1, 2012 // 分类:Windows // No Comments

提及Putty大家都不陌生,Putty自带的工具Plink同样强大,本文就简单介绍下几种用法。
Plink使用参数:

Usage: plink [options] [user@]host [command]
Options:
  -V        print version information and exit
  -pgpfp    print PGP key fingerprints and exit
  -v        show verbose messages
  -load sessname  Load settings from saved session
  -ssh -telnet -rlogin -raw -serial
            force use of a particular protocol
  -P port   connect to specified port
  -l user   connect with specified username
  -batch    disable all interactive prompts
The following options only apply to SSH connections:
  -pw passw login with specified password
  -D [listen-IP:]listen-port
            Dynamic SOCKS-based port forwarding
  -L [listen-IP:]listen-port:host:port
            Forward local port to remote address
  -R [listen-IP:]listen-port:host:port
            Forward remote port to local address
  -X -x     enable / disable X11 forwarding
  -A -a     enable / disable agent forwarding
  -t -T     enable / disable pty allocation
  -1 -2     force use of particular protocol version
  -4 -6     force use of IPv4 or IPv6
  -C        enable compression
  -i key    private key file for authentication
  -noagent  disable use of Pageant
  -agent    enable use of Pageant
  -m file   read remote command(s) from file
  -s        remote command is an SSH subsystem (SSH-2 only)
  -N        don't start a shell/command (SSH-2 only)
  -nc host:port
            open tunnel in place of session (SSH-2 only)
  -sercfg configuration-string (e.g. 19200,8,n,1,X)
            Specify the serial configuration (serial only)

1.动态转发端口,可用于代*理上网哦。

plink -N -D 127.0.0.1:7070 root@192.168.1.2 -pw passwd

2.转发本地端口到远程服务器

plink -N -L 127.0.0.1:2222:google.com:80 root@haiyun.me -pw passwd
#通过haiyun.me将本地2222端口转发google.com 80端口,浏览器打开127.0.0.1:2222就是谷歌网站

3.登录ssh服务器并依次执行文件内的命令。

plink -m cmd.txt root@www.haiyun.me -pw passwd

4.内网穿透,转发远程服务器端口到本地端口,远程监听指定地址查看:https://www.haiyun.me/archives/1010.html

plink -N -R 0.0.0.0:2222:localhost:3389 root@haiyun.me -pw passwd
#转发haiyun.me 2222端口到本地3389端口

Iptables日记模块LOG使用

发布时间:April 30, 2012 // 分类:Iptables // No Comments

Iptables匹配相应规则后会触发一个动作,filter和nat表一般常用的有以下目标操作。

ACCEPT #允许数据包通过
DROP #丢弃数据包,不对该数据包进一步处理
REFECT #丢弃数据包,同时发送响应报文
--reject-with tcp-reset 返回tcp重置
--reject-with icmp-net-unreachable 返回网络不可达
--reject-with icmp-host-unreachable  返回主机不可达
RETURN #转到其它链处理
LOG #将数据包信息记录到syslog

本文就记录下LOG规则的使用,示例:进入的tcp端口为80的数据包记录到日记,错误级别err,描述前缀为INPUT,记录IP/TCP相关信息。

#https://www.haiyun.me
modprobe ipt_LOG #加载模块
iptables -A INPUT -p tcp --dport 80 -j LOG --log-level err  --log-prefix "INPUT" --log-ip-options --log-tcp-sequence
--log-level #错误级别
--log-prefix "INPUT" #描述前缀
--log-ip-options #记录IP信息
--log-tcp-sequence #记录TCP序列号

然后访问服务器80端口测试,通过dmesg查看记录的信息如下:

INPUTIN=eth0 OUT= MAC=00:0c:29:73:e0:19:8c:89:a5:65:3a:4a:08:00 SRC=192.168.1.16 DST=192.168.1.2 \
LEN=522 TOS=0x00 PREC=0x00 TTL=128 ID=27499 DF PROTO=TCP SPT=5430 DPT=80 SEQ=3847892455 ACK=3435733082 WINDOW=16344 RES=0x00 ACK PSH URGP=0 

还可以修改syslog将日志写入到文件。

vim /etc/syslog.conf #添加以下内容
kern.err           /var/log/iptables #日志文件路径
/etc/init.d/syslog restart #重启syslog服务

Windows 2003用IP安全策略限制udp-flood发包脚本

发布时间:April 29, 2012 // 分类:网络安全,Windows // No Comments

之前有介绍Linux下通过iptables限制UDP发包,这次记录下Windows 2003的实现方法。
新建bat脚本,添加以下内容,然后点击运行,点此下载禁止UDP发包脚本

:Created by https://www.haiyun.me
:DROP UDP Flood
@echo off
cls
:获取DNS地址
for /f "delims=: tokens=2" %%a in ('ipconfig /all ^| find /i "dns"') do set DNSIP=%%a
)
:新建IP安装策略禁止UDP
netsh ipsec static add policy name=禁止UDP description=允许DNS,拒绝其它UDP外出
:新建IP安全规则
netsh ipsec static add filterlist name=允许UDP
netsh ipsec static add filterlist name=拒绝UDP
:新建IP筛选器
netsh ipsec static add filter filterlist=允许UDP srcaddr=me dstaddr=%DNSIP% description=允许DNS查询 protocol=udp mirrored=yes dstport=53
netsh ipsec static add filter filterlist=拒绝UDP srcaddr=me dstaddr=any description=禁止UDP外出 protocol=udp mirrored=yes
:新建IP筛选器操作
netsh ipsec static add filteraction name=允许DNS查询 action=permit 
netsh ipsec static add filteraction name=拒绝UDP外出 action=block 
:封装策略
netsh ipsec static add rule name=允许规则 policy=禁止UDP  filterlist=允许UDP filteraction=允许DNS查询
netsh ipsec static add rule name=拒绝规则 policy=禁止UDP  filterlist=拒绝UDP filteraction=拒绝UDP外出
:应用IP安全策略
netsh ipsec static set policy name=禁止UDP assign=y

Linux/Centos服务器ssh安全设置

发布时间:April 28, 2012 // 分类:网络安全 // 2 Comments

互联网很危险,SSH做为服务器的大门一定要做好安全设置,我曾经做过测试,一台VPS开启ssh服务监听默认端口22,几天后secure日记分割了10多个,统计最新几小时的恶意登录失败的IP数据如下:
ssh恶意登录统计.png
真是个恐怖的次数,蛋痛的人太多了,如果密码稍简单就被破解了,如需自动封IP请参考:iptables自动封IP防SSH被暴力破解
1.更改默认端口,网上很多针对22端口扫描的软件,这样不会被误伤。

#https://www.haiyun.me
sed -i 's/#Port 22/Port 33333/g' /etc/ssh/sshd_config 
#更改ssh端口为3333

2.禁止root登录,新建普通用户登录,登录后可su -转入root账户,让恶意登录者无法猜测用户名。

useradd onovps #新建用户名
passwd onovps #设置密码
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
#禁止root登录
service sshd restart #重启ssh服务生效

3.限制登录失败次数并锁定

vim /etc/pam.d/login

在#%PAM-1.0下面添加:

auth required pam_tally2.so deny=5 unlock_time=180 #登录失败5次锁定180秒,不包含root
auth required pam_tally2.so deny=5 unlock_time=180 even_deny_root root_unlock_time=180 #包含root

4.允许特定的用户登录,编辑ssh配置文件:

vim /etc/ssh/sshd_config 
AllowUsers user 
#默认允许全部,多个用户以空格隔开,也可拒绝特定用户登录。
DenyUsers user

5.设置重复验证次数,默认3次:

MaxAuthTries 0
#错误一次即断开连接

6.直接用Iptables封闭ssh端口,为ssh服务器设置开门钥匙,有此其它都是浮云啦。。。。
一般做这些设置就足够了,也可设置为禁用密码用密钥登录,不同客户端方法不同,以后再写吧。

分类
最新文章
最近回复
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...
  • 孤狼: 擦。。。。apcupsd会失联 nut在冲到到100的时候会ONBATT进入关机状态,我想想办...
  • 海运: 网络,找到相应的url编辑重发请求,firefox有此功能,其它未知。
  • knetxp: 用浏览器F12网络拦截或监听后编辑重发请求,修改url中的set为set_super,将POS...
  • Albert: 啊啊啊啊啊啊啊啊啊 我太激动了,终于好了英文区搜索了半天,翻遍了 pve 论坛没找到好方法,博...
归档