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

NGINX 配置404错误页面

NGINX 配置404错误页面

1、创建自己的404.html页面 

2、编辑nginx.conf在http定义区域加入:fastcgi_intercept_errors on; 

3、编辑nginx.conf或虚拟主机的域名.conf在server 区域加入:

error_page 404 /404.html;

4、测试nginx.conf正确性:

#/usr/local/nginx/sbin/nginx -t

the configuration file /opt/nginx/conf/nginx.conf syntax is ok 
configuration file /opt/nginx/conf/nginx.conf test is successful

5、重启nginx生效

#/usr/local/nginx/sbin/nginx -s reload

6、502 等错误可以用同样的方法来配置。  

error_page  500 502 503 504  /50x.html; 

Nginx 404 (1)

Nginx 404 (2)

开启Nginx的目录文件列表功能,目录索引(支持中文)。

开启Nginx的目录文件列表功能,目录索引(支持中文)。
(LNMP一键安装包http://www.lnmp.org/)
1、修改nginx.conf配置文件
nginx.conf文件(/usr/local/nginx/conf/nginx.conf)
在http段中加入
autoindex on;
autoindex_exact_size off;(默认on,改为off后显示出文件的大概大小)
autoindex_localtime on;(默认off,显示GMT时间;改为on后,显示文件的服务器时间)
2、修改系统默认语言与编码(中文)
#vi /etc/sysconfig/i18n
  1 LANG="en_US.UTF-8"
  2 SYSFONT="latarcyrheb-sun16"
将LANG="en_US.UTF-8" 修改成 LANG="zh_CN.UTF-8"
ESC退出编辑,:wq或输入两次大写字母ZZ,保存并退出vi。
#reboot
3、修改nginx.conf文件里配置的字符集为utf-8
server 
{
listen 80;
charset utf-8;
}
重启Nginx  /etc/init.d/nginx restart,或重启LNMP  /root/lnmp restart。

Nginx Index (1) Nginx Index (2)

PureFTPd 553 无法开启那个文件: Permission denied

PureFTPd 553 无法开启那个文件: Permission denied

一:PureFTPd Web面板中,修改或创建账户的时候,修改UID和GID都为www,或者是其他任何用户和用户组。
二:Root账户登录SSH
#chown -R www /home/wwwroot/wp.zhangfangzhou.cn/
#chgrp -R www /home/wwwroot/wp.zhangfangzhou.cn/
Pureftpd的重启命令:
#/etc/init.d/pureftpd restart
chown(改变文件拥有者)
chgrp(改变文件所属用户组)
chmod(更改文件属性)

PureFTPd (09) PureFTPd (10) PureFTPd (11) PureFTPd (12)

Nginx WordPress伪静态rewrite

Nginx WordPress伪静态rewrite

rewrite ^.*/files/(.*)$ /wp-includes/ms-files.php?file=$1 last;
if (!-e $request_filename) {
rewrite ^.+?(/wp-.*) $1 last;
rewrite ^.+?(/.*\.php)$ $1 last;
rewrite ^ /index.php last;
}
将上述代码插入:
(1)/usr/local/nginx/conf/vhosts.conf  (普通)
(2)/usr/local/nginx/conf/vhost/domain.conf    (LNMP http://lnmp.org/)
 server {…} 中,
 重启 nginx(/etc/init.d/nginx restart) 即可生效。

phpStudy (10)

LNMP下实现Discuz 伪静态rewrite

LNMP下实现Discuz 伪静态rewrite(LNMP一键安装包http://www.lnmp.org/)
第一步,修改conf文件,找到站点.conf文件,LNMP在/usr/local/nginx/conf/vhost下
第二步,登陆Discuz后台,全局,SEO设置,全部选中,提交,查看当前的 Rewrite 规则,复制。
第三步,将站点.conf下载到本地编辑,替换include discuz.conf;为刚才复制的Rewrite 规则
第四部,重启LNMP,登陆SSH使用如下命令:/root/lnmp restart

LNMP Discuz (0) LNMP Discuz (1) LNMP Discuz (2) LNMP Discuz (3) LNMP Discuz (4) LNMP Discuz (5)