Nginx1.90 nginx_stream 做TCP代理和协议负载均衡

Nginx1.90做TCP代理和协议负载均衡的功能
nginx从1.9.0开始增加了stream模块(ngx_stream_core_module),默认configure不包含该模块,需要在configure的时候加上--with-stream

./configure --prefix=/usr/local/nginx --user=www --group=www \
--add-module=/root/ngx_http_google_filter_module \
--add-module=/root/ngx_http_substitutions_filter_module \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-ipv6 \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_flv_module \
--with-http_sub_module \
--with-stream

配置文件很简单,最基本的

.....................
events {
use epoll;
worker_connections 51200;
}
stream {
server {
listen 2002;
proxy_pass 123.123.123.123:3389;
}

server {
listen 2003;
proxy_pass 123.123.123.123:22;
}
}

http {
.....................

需要在防火墙允许相应的端口通过。这样可以反代远程桌面3389端口或者其他固定的TCP端口,比iptables转发或者虚拟专用网络连接来管理国外Windows或者Linux服务器要方便不少。

当然,该模块最重要的功能是支持TCP负载均衡,比如远程多台mysql负载均衡。

stream {
upstream mysql {
server 1.1.1.1:3306;
server 2.2.2.2:3306;
server 3.3.3.3:3306;
}
server {
listen 3306;
proxy_pass mysql;
}
}

官方文档http://nginx.org/en/docs/stream/ngx_stream_core_module.html

Nginx配置https服务器,http重定向到https

Nginx配置https服务器,http重定向到https
server {
listen 443;
server_name www.zhangfangzhou.cn;
index index.html index.htm index.php default.html default.htm default.php;
root /home/zhangfangzhou.cn;
ssl on;
ssl_certificate /usr/local/nginx/conf/vhost/zhangfangzhou_cn.crt;
ssl_certificate_key /usr/local/nginx/conf/vhost/server.key;

include wordpress.conf;
error_page 404 /404.html;
location ~ [^/]\.php(/|$)
{
# comment try_files $uri =404; to enable pathinfo
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
#include pathinfo.conf;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

access_log /home/wwwlogs/zhangfangzhou.cn.log access;
error_log /home/wwwlogs/zhangfangzhou.cn.error.log error;

location /wp-content/uploads {
location ~ .*\.(php)?$ {
deny all;
}
}
#禁止附件目录运行PHP
#附件目录限制ip访问
location ^~ /wp-content/uploads/ {
deny 42.159.103.191;
}
#给 wp-login.php 添加 htpasswd 验证
location ^~ /wp-login.php {
location ~ [^/]\.php(/|$) {
# comment try_files $uri =404; to enable pathinfo
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
#include pathinfo.conf;
}
auth_basic "Please Input Your PassWord";
auth_basic_user_file /usr/local/nginx/conf/vhost/.htpasswd;
}

}

#重定向zhangfangzhou.cn到https://www.zhangfangzhou.cn
server {
listen 80;
server_name zhangfangzhou.cn;
return 301 https://www.zhangfangzhou.cn$request_uri;
}

#重定向www.zhangfangzhou.cn到https://www.zhangfangzhou.cn
server {
listen 80;
server_name www.zhangfangzhou.cn;
return 301 https://www.zhangfangzhou.cn$request_uri;
}

可能出现"网页生成了过多的重定向"而无法打开网站,解决办法:调换下面两个server的位置。

https