在nginx下配置自动虚拟主机

作者 : admin 于 2010年01月09日, 00:33:37
2010
01-9

以前用apache,很多虚拟主机的时候,用 mod_vhost_alias 模块去解决。nginx似乎没有这样的功能。

原来为了做这个功能,我用python写了一堆脚本,去自动管理nginx的配置文件,结果还是不理想。频繁重写配置文件,频繁重启,总会出现点问题。

在nginx的0.8.*下,发现了这样的功能:http://wiki.nginx.org/NginxHttpCoreModule

Since nginx 0.8.25 named captures can be used in server_name:
server {
server_name ~^(www\.)?(?.+)$;
location / {
root /sites/$domain;
}
}

大喜,于是乎做出如下配置,实现了nginx下自动虚拟主机的功能:

  1. server {
  2.      listen       80;
  3.      server_name  ~^(?P<domainname>.+)\.autovhost\.sunboyu\.cn$;
  4.      location / {
  5.          #autoindex  on;
  6.          root   /home/vhost/$domainname;
  7.          index  index.html index.htm;
  8.      }
  9.      access_log /home/autovhost.sunboyu.cn.log main;
  10. }
  11. </domainname>

测试通过。

我的开源虚拟主机管理系统nginx版本指日可待。

nginx做反向代理的配置

作者 : admin 于 2009年12月16日, 18:39:13
2009
12-16

感谢铎哥的配置,感谢宴哥解决ssl连接的问题。

  1. server
  2.  {
  3.         listen      8181;
  4.         resolver 202.96.64.68;
  5.         location /
  6.         {
  7.             proxy_pass http://$http_host$request_uri;
  8.             proxy_redirect          off;
  9.             proxy_set_header        Host            $host;
  10.             proxy_set_header        X-Real-IP       $remote_addr;
  11.             proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
  12.             client_max_body_size    10m;
  13.             client_body_buffer_size 128k;
  14.             proxy_connect_timeout   90;
  15.             proxy_send_timeout      90;
  16.             proxy_read_timeout      90;
  17.             proxy_buffers           32 4k;
  18.         }
  19.         access_log /home/proxy.log;
  20.  }

django+nginx的部分配置

作者 : admin 于 2009年12月16日, 11:26:55
2009
12-16

nginx的配置,特别感谢爱词霸吕同学,发扬了开源共享的精神,大大缩短了我的调试成本。

  1. server {
  2.     listen 80;
  3.     server_name python.sunboyu.cn;
  4.     location / {
  5.           fastcgi_pass 127.0.0.1:8000;
  6.           fastcgi_buffers      16  128k;
  7.           fastcgi_ignore_client_abort  on;
  8.           fastcgi_read_timeout 60;
  9.  
  10.           fastcgi_param PATH_INFO $fastcgi_script_name;
  11.           fastcgi_param REQUEST_METHOD $request_method;
  12.           fastcgi_param QUERY_STRING $query_string;
  13.           fastcgi_param CONTENT_TYPE $content_type;
  14.           fastcgi_param CONTENT_LENGTH $content_length;
  15.           fastcgi_param SERVER_PROTOCOL  $server_protocol;
  16.           fastcgi_param SERVER_PORT      $server_port;
  17.           fastcgi_param SERVER_NAME  $server_name;
  18.  
  19.           fastcgi_pass_header Authorization;
  20.           fastcgi_intercept_errors off;
  21.  
  22.     }
  23. }

同时附上一个额外的文档,nginx变量跟cgi协议的对应关系。
注:在配置中,并不是所有的变量必须加上,而是根据环境选择其中应该有的变量,至于具体加哪些变量,得求助高人了。

  1. #    Fast CGI param reference
  2. #    fastcgi_param    SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  3. #    fastcgi_param    QUERY_STRING  $query_string;
  4. #    fastcgi_param    REQUEST_METHOD  $request_method;
  5. #    fastcgi_param    CONTENT_TYPE  $content_type;
  6. #    fastcgi_param    CONTENT_LENGTH  $content_length;
  7. #    fastcgi_param    GATEWAY_INTERFACE  CGI/1.1;
  8. #    fastcgi_param    SERVER_SOFTWARE  nginx;
  9. #    fastcgi_param    SCRIPT_NAME  $fastcgi_script_name;
  10. #    fastcgi_param    REQUEST_URI  $request_uri;
  11. #    fastcgi_param    DOCUMENT_URI  $document_uri;
  12. #    fastcgi_param    DOCUMENT_ROOT  $document_root;
  13. #    fastcgi_param    SERVER_PROTOCOL  $server_protocol;
  14. #    fastcgi_param    REMOTE_ADDR  $remote_addr;
  15. #    fastcgi_param    REMOTE_PORT  $remote_port;
  16. #    fastcgi_param    SERVER_ADDR  $server_addr;
  17. #    fastcgi_param    SERVER_PORT  $server_port;
  18. #    fastcgi_param    SERVER_NAME  $server_name;

针对尚趣网(vsuch.com)的简单优化

作者 : admin 于 2009年06月12日, 15:42:59
2009
06-12

最近,一友找我去优化apache,了解后,是vsuch.com网站的问题。

vsuch网站使用lamp+windows混合平台(汗),追究历史,原来网站用.net编写,后用php重构了整个网站。

整改前 Mysql运行在linux机器上,php运行在windows平台上,中间局域网方式连接。

网站日访问量不小,alexa排名6800,windows平台明显抗不住,经常莫名其妙的问题。(题外话:我最早维护的服务器也是windows,apache在上边很不稳定)

了解后,我感觉一台服务器就足矣,放弃了apache的方案,安装了nginx+php,数据库依然沿用原来的。

整改后,linux负载稍稍升高,mysql负载不变,http的负载在nginx下并没有表现出多高。顺利完成了迁移。

后公司又开通了cdn服务(有钱),速度有了很明显的提升。

-----------------------------------------

根据其公司目前的技术结构,我提出了很多优化和整改的想法,希望每个创业网站都能走好。

-----------------------------------------

singlekui@gmail.com: 孙. 谢谢你帮忙了. 我拿100元给买包烟抽抽. :)
我: ……
singlekui@gmail.com: 可以吗.也别介意呀.
我: 算了
就当玩了
singlekui@gmail.com: 啊..
说了给你点报酬的呢.
9:55 我: 就当玩了

做技术的,很多时候要学会一笑了之。

wordpress在不同webserver下的重写规则和配置

作者 : admin 于 2009年04月07日, 21:22:07
2009
04-7

apache下

  1. #.htaccess
  2. RewriteEngine On
  3. RewriteBase /
  4. RewriteCond %{REQUEST_FILENAME} !-f
  5. RewriteCond %{REQUEST_FILENAME} !-d
  6. RewriteRule . /index.php [L]
  7. #httpd.conf
  8. Options ExecCGI FollowSymLinks
  9. AllowOverride All
  10. Order allow,deny
  11. allow from all

nginx下

  1. #nginx.conf
  2. location / {
  3.         if (!-f $request_filename){
  4.             rewrite (.*) /index.php;
  5.         }
  6.     }

还是Nginx对抗负载的能力强

作者 : admin 于 2009年04月07日, 14:45:11
2009
04-7

编译了老半天的apache,又做了很多调整和优化,依然抗不住死机,其实负载也不是很大,但内存CPU就是出奇的高,无奈,装上nginx,负载一下子下来了。

顺便提一下,我PC级的服务器。

感谢  http://www.51fit.com/ 友情提供测试数据

计划重新部署apache,调整工作模式。

试用webmin来管理nginx

作者 : admin 于 2008年05月18日, 12:30:44
2008
05-18

webmin没有nginx的管理模块,可以使用自定义命令来实现

关闭nginx:killall nginx 使用root执行

启动nginx:/usr/local/nginx/sbin/nginx

重启:两个命令连起来就可以,也可以写个简单shell

编辑配置文件:增加编辑文件命令 /usr/local/nginx/conf/**.conf

用着还挺爽的,简单方便

Nginx下WordPress的重写配置

作者 : admin 于 2008年05月18日, 12:30:14
2008
05-18

nginx居然不兼容apache的.htaccess文件,无奈在配置文件中修改.

server
{
listen  80;

location / {
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}

server_name sunboyu.cn www.sunboyu.cn *.sunboyu.cn somecode.cn www.somecode.cn *.somecode.cn;
index   index.html index.htm index.php;
root    /***/;
access_log  /***/host.access.log;
}

这样即可

centos5安装nginx+mysql+php fastcgi模式

作者 : admin 于 2008年05月18日, 12:20:01
2008
05-18
原教程 http://blog.s135.com/read.php/314.htm

安装环境:centos5.0 基本系统+开发工具

安装xml组件
yum install libxml2 libxml2-devel

编译安装 php
./configure –enable-fastcgi –enable-force-cgi-redirect
make && make install
————————————————————————–
[root@localhost php-5.2.5]# make install
Installing PHP SAPI module: cgi
Installing PHP CGI binary: /usr/local/bin/
Installing PHP CLI binary: /usr/local/bin/
Installing PHP CLI man page: /usr/local/man/man1/
Installing build environment: /usr/local/lib/php/build/
Installing header files: /usr/local/include/php/
Installing helper programs: /usr/local/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment: /usr/local/lib/php/
[PEAR] Console_Getopt – installed: 1.2.3
[PEAR] Archive_Tar – installed: 1.3.2
[PEAR] Structures_Graph- installed: 1.0.2
pear/PEAR can optionally use package “pear/XML_RPC” (version >= 1.4.0)
[PEAR] PEAR – installed: 1.6.1
Wrote PEAR system config file at: /usr/local/etc/pear.conf
You may want to add: /usr/local/lib/php to your php.ini include_path
Installing PDO headers: /usr/local/include/php/ext/pdo/
————————————————————————–
make test

yum安装mysql
yum install mysql mysql-devel mysql-server

复制spawn-fcgi 至 /usr/local/bin/
chmod +x spawn-fcgi

/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www
使用spawn-fcgi监听127.0.0.1的10080端口 进程10 用户www
————————————————————————–
[root@localhost bin]# spawn-fcgi -a 127.0.0.1 -p 10080 -C 10 -u www -f php-cgi
X-Powered-By: PHP/5.2.5
Content-type: text/html
spawn-fcgi.c.211: child exited with: 0, Success
————————————————————————–
[root@localhost bin]# spawn-fcgi -a 127.0.0.1 -p 10080 -C 10 -u www -f php-cgi
spawn-fcgi.c.190: child spawned successfully: PID: 27936
————————————————————————–
安装pcre
yum install pcre pcre-devel
编译安装 nginx
./configure –user=www –group=www
————————————————————————–
Configuration summary
+ threads are not used
+ using system PCRE library
+ OpenSSL library is not used
+ md5 library is not used
+ sha1 library is not used
+ using system zlib library
nginx path prefix: “/usr/local/nginx”
nginx binary file: “/usr/local/nginx/sbin/nginx”
nginx configuration file: “/usr/local/nginx/conf/nginx.conf”
nginx pid file: “/usr/local/nginx/logs/nginx.pid”
nginx error log file: “/usr/local/nginx/logs/error.log”
nginx http access log file: “/usr/local/nginx/logs/access.log”
nginx http client request body temporary files: “/usr/local/nginx/client_body_temp”
nginx http proxy temporary files: “/usr/local/nginx/proxy_temp”
nginx http fastcgi temporary files: “/usr/local/nginx/fastcgi_temp”
————————————————————————–
make && make install

touche /usr/local/nginx/conf/fcgi.conf
内容为
————————————————————————–
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with –enable-force-cgi-redirect
#fastcgi_param REDIRECT_STATUS 200;
————————————————————————–
启动 nginx
ulimit -SHn 51200
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

配置开机自动启动Nginx + PHP
vi /etc/rc.local
在末尾增加以下内容:
ulimit -SHn 51200
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 64 -u www -f /usr/local/bin/php-cgi
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

优化Linux内核参数
vi /etc/sysctl.conf
在末尾增加以下内容:
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000 65000

使配置立即生效:
/sbin/sysctl -p
在不停止Nginx服务的情况下平滑变更Nginx配置
修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
/usr/local/webserver/nginx/sbin/nginx -t

这时,输入以下命令查看Nginx主进程号:
ps -ef | grep “nginx: master process” | grep -v “grep” | awk -F ‘ ‘ ‘{print $2}’

至于日常维护,还不太清楚,咱们下次分解

—————————————————–
Nginx的编译参数如下:
[root@localhost]#./configure –prefix=/usr/local/server/nginx –with-openssl=/usr/include \
–with-pcre=/usr/include/pcre/ –with-http_stub_status_module –without-http_memcached_module \
–without-http_fastcgi_module –without-http_rewrite_module –without-http_map_module \
–without-http_geo_module –without-http_autoindex_module