Posts Tagged ‘Nginx’

Gentoo下Nginx使用GeoIP模块为Django提供GEO信息

Tuesday, July 27th, 2010

首先要在Gentoo下安装编译Nginx,需要加入GeoIP模块,请参考另一篇文章《Gentoo 安装编译Nginx》。在安装的过程中应该是会自动安装dev-libs/geoip这个包,如果没有的话使用emerge安装:

emerge -avt dev-libs/geoip

下载MaxMind的GEO数据,解压缩放在/etc/nginx目录下:

#Get the free database of geo_city
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
mv GeoLiteCity.dat /etc/nginx
#Get the free database of geo_coundty
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gunzip GeoIP.dat.gz
mv GeoIP.dat /etc/nginx

编辑/etc/nginx/nginx.con文件,加入GeoIP的支持:

http {
    geoip_country  /etc/nginx/GeoIP.dat;
    geoip_city     /etc/nginx/GeoLiteCity.dat;
}

编辑django的fastcgi_param:

fastcgi_param GEOIP_COUNTRY_CODE $geoip_city_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
fastcgi_param GEOIP_REGION $geoip_region;
fastcgi_param GEOIP_CITY $geoip_city;
fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
fastcgi_param GEOIP_LAT $geoip_latitude;
fastcgi_param GEOIP_LNG $geoip_longitude;

在Django的使用方法:

def index(request):
    return HttpResponse(request.META['GEOIP_COUNTRY_CODE'])

Gentoo 安装编译Nginx

Friday, April 2nd, 2010

在Gentoo下使用Nginx,总是涉及到编译Nginx的HTTP Module。每次系统更新都要重新设置USES、NGINX_MODULES_HTTP很麻烦,把这些关键字写在配置文件里面就不容易出错。

首先在/etc/portage/package.keywords下加入如下行,这样就可以使用最新的dev版本了:

www-servers/nginx ~x86

/etc/portage/package.use文件中加入如下内容,是否使用-ipv6要看安装Nginx的服务器是否支持IPV6:

www-servers/nginx aio libatomic -ipv6

最后在/etc/make.conf加入对HTTP Module的设定:

NGINX_MODULES_HTTP="addition fastcgi flv geo geoip sub access auth_basic rewrite"

最后使用emerge命令安装Nginx:

emerge -avt nginx

安装完成后可以使用命令查看nginx的编译参数:

nginx -V

Nginx在Mac上的安装配置

Tuesday, January 12th, 2010

在Mac上使用macport安装和配置Nginx非常方便,首先安装nginx:

sudo port install nginx

安装完成后启动nginx会说没有nginx.conf文件,到/opt/local/etc/nginx目录下看到以下几个文件:

fastcgi_params.example koi-utf koi-win mime.types.example nginx.conf.example win-utf

直接复制example文件:

sudo mv mime.types.example mime.types
sudo mv nginx.conf.example nginx.conf

启动nginx:

sudo nginx

访问http://127.0.0.1/,就可以看到Nginx的Welcome页面。

CouchDB在Nginx下设置代理访问

Friday, December 4th, 2009

一晃眼发现CouchDB都更新到0.10.1了,自从上次在Ubuntu Server上折腾0.10.0未遂后就一直没有研究它的安装配置。
CouchDB用Nginx做代理访问的设置:

    server{
        listen 5000;
        server_name couchdb;
        access_log /var/log/nginx/couchdb.access.log;
        error_log /var/log/nginx/couchdb.error_log info;
 
        location / {
            proxy_pass http://localhost:5984;
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
 
    }

Gentoo下构建Nginx+Thin+Rails 运行环境

Friday, November 27th, 2009

学习什么新的东西,一般都是从构建环境开始的。虽然还没有搞明白怎么用rails开发一个站点,对ruby也不是很清楚,还是先根据网上的文档搭建了一个运行的环境。

Gentoo安装后自带ruby 1.8.7版本,用emerge命令安装上rubygems后一切ruby相关的包都用gem来搞定。thin是ruby的一个轻量级的web框架,在Gentoo下还是被Marked,所以也用gem安装。gem的安装命令:

gem install rails thin rack eventmachine

在rails的项目目录/lib/tasks下创建一个thin.rake文件:

namespace :thin do
  namespace :cluster do
  desc 'Start thin cluster'
    task :start => :environment do
      `cd #{RAILS_ROOT}`
      port_range = RAILS_ENV == 'developmet' ? 3 : 8
      (ENV['SIZE'] ? ENV['SIZE'].to_i : 4).times do |i|
        Thread.new do
          port = ENV['PORT'] ? ENV['PORT'].to_i + i : ("#{port_range}%03d" % i)
          str  = "thin start -d -p#{port} -Ptmp/pids/thin-#{port}.pid"
          str += " -e#{RAILS_ENV}"
          puts str
          puts "Starting server on port #{port}…"
          `#{str}`
        end
      end
    end
  desc 'Stop all thin clusters'
    task :stop => :environment do
      `cd #{RAILS_ROOT}`
      Dir.new("#{RAILS_ROOT}/tmp/pids").each do |file|
        Thread.new do
          if file.starts_with?("thin-")
            str  = "thin stop -Ptmp/pids/#{file}"
            puts "Stopping server on port #{file[/\d+/]}…"
            `#{str}`
          end
        end
      end
    end
  end
end

启动和关闭thin服务的命令:

rake thin:cluster:start RAILS_ENV=production SIZE=3 PORT=8000
rake thin:cluster:stop

修改Ngin的配置文件/etc/nginx/nginx.conf:

    upstream thin {
        server 127.0.0.1:8000;
        server 127.0.0.1:8001;
        server 127.0.0.1:8002;
    }
 
    server {
        listen   9000;
        server_name  localhost;
        access_log  /var/log/nginx/rails.access.log;
        error_log   /var/log/nginx/rails.error_log info;
 
        root /root/project/xxxxx;
 
        location / {
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            if (-f $request_filename/index.html) {
                rewrite (.*) $1/index.html break;
            }
            if (-f $request_filename.html) {
                rewrite (.*) $1.html break;
            }
            if (!-f $request_filename) {
                proxy_pass http://thin;
                break;
            }
        }
    }

启动Nginx,访问http://localhost:9000看看是否显示rails的Welcome aboard页面。