debian安装nginx作反向代理

来源:互联网 发布:linux服务器清屏命令 编辑:程序博客网 时间:2024/06/07 01:40

http://www.blogjava.net/BearRui/archive/2010/01/29/web_performance_server.html



网上有很多教程,让nginx作php的fastcgi引擎,可是好像还不太成熟。而nginx的最大功能是反向代理与负载平衡.

这里仅作web server 的动静分离.


1,安装nginx

在debian中安装nginx相当方便

apt-get install nginx



2,基本设置

http://www.hdj.me/tag/nginx%E5%AE%89%E5%85%A8%E9%85%8D%E7%BD%AE


取消版本信息
nginx.conf
添加 server_tokens off;

http {
//……省略
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
server_tokens off;
//…….省略
}




http://www.linuxidc.com/Linux/2011-04/34368.htm
限制ip访问

#禁止IP访问
server
{
listen 80 default;
server_name _;
return 500;
}


3,反向设置


http://www.imfeng.com/nginx-nginx-conf-des/



#这个是反向代理的例子
server { # simple reverse-proxy
listen 80;
server_name domain2.com www.domain2.com;
access_log logs/domain2.access.log main;

#静态文件,nginx自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/big.server.com/htdocs;
#过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
expires 30d;
}

#比较直观的设置是
#location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$


#把请求转发给后台web服务器,反向代理和fastcgi的区别是,反向代理后面是web服务器,fastcgi后台是fasstcgi监听进程,当然,协议也不一样。
location ~ .php$ {
        proxy_pass        http://www.codesky.net;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
}
}



ps.优化设置

http://www.sishaofeng.com/nginx/the-apache-nginx-front-end-back-end-server.html




4,后端apache,安装mod_rpaf,接收反向过来的真实ip

http://phpsuxx.blogspot.kr/2009/11/modrpaf-debian.html

http://www.yyphs.com/post/423.html
http://www.net527.cn/a/caozuoxitong/Linux/2011/0201/16292.html



安装mod

apt-get install libapache2-mod-rpaf -y --force-yes


修改反向代理的IP设置

vi /etc/apache2/mods-enabled/rpaf.conf

RPAFproxy_ips 127.0.0.1





5,负载设置
http://www.codesky.net/article/201201/173239.html









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

这里附一份nginx+php5-fpm的安装过程

http://www.5ilinux.com/2011/04/debian6lemp01.html

http://blog.it580.com/nginx-php-fpm-mysql%E5%AE%89%E8%A3%85debian-squeeze-vps



网上有很多利用spawn-fcgi作nginx的后台php解释器,不过设置没有这个简单.



1,添加源Debian 6(Squeeze)

deb http://packages.dotdeb.org stable all


要求key

wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | apt-key add -
rm dotdeb.gpg


我不用也可以装上的,apt-get update



2,安装

apt-get install nginx php5-cli php5-cgi php5-fpm3,设置

修改nginx的配置文件

vi /etc/nginx/sites-available/default

添加: location ~ \.php$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;include fastcgi_params;}

4,启动服务/etc/init.d/php5-fpm start/etc/init.d/nginx restart




5,安装https

http://library.linode.com/web-servers/nginx/configuration/ssl


mkdir /srv/ssl/cd /srv/ssl/openssl req -new -x509 -days 365 -nodes -out /srv/ssl/nginx.pem -keyout /srv/ssl/nginx.key

nginx.conf
server {      # [...]      listen 443;      ssl on;      ssl_certificate      /srv/ssl/nginx.pem;      ssl_certificate_key  /srv/ssl/nginx.key;      # [...]}



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

再附一个debian安装memcache的步骤

http://blog.csdn.net/rainysia/article/details/6780554

http://hi.baidu.com/aegkey/item/a1ab82cd61255e0e0ad93ab0

安装

apt-get install memcached php5-memcache 

测试

  1. < ?php  
  2. $mem = new Memcache;  
  3. $mem->connect(“127.0.0.1″, 11211);  
  4. $mem->set(‘key’, ‘This is a test!’, 0, 60);  
  5. $val = $mem->get(‘key’);  
  6. echo $val;  
  7. ?>