nginx 备忘

1)域名恶意绑定

今天发现一个域名被恶意绑定IP了,解决办法是在Nginx中加一个默认的server。
看过这篇How nginx processes a request文章应该知道,Nginx会找到一个server配置来处理请求:

  server {
    listen       80;
    server_name  nginx.org  www.nginx.org;
    ...
  }

上面的配置文件如果不是来自nginx.org或者www.nginx.org的话也会走这个server,这是因为我们没有指定default_server

  server{
    listen       80;
    server_name  nginx.org  www.nginx.org;
    ...
  }

  server{
    listen       80 default_server;
    rewrite   ^(.*) http://www.nginx.org permanent;
    ...
  }

上面的配置文件就可以了,如果不是来自指定域名的请求,就会走下面的default_server,然后再让它永久重定向到我们网站就解决域名恶意绑定的问题了。

2)gzip

看文档 HttpGzipModule模块

3)alias vs root

location  /i/ {
  alias  /spool/w3/images/;
}

“/i/top.gif” => “/spool/w3/images/top.gif”

location  /i/ {
  root  /spool/w3/images/;
}

“/i/top.gif” => “/spool/w3/images/i/top.gif”

deployment capistrano unicorn nginx rails3

How deploy rails3 application using capistrano,unicorn,and nginx.
As we know,Capistrano is a auto-deploy tool kit just like Rake.

First,Let us deploy with unicorn and nginx on the server.
1)install nginx

sudo apt-get install nginx

2)install unicorn

[sudo] gem install unicorn

3)config unicorn under nginx
Find where is your nginx directory. Mine is /etc/nginx.

nginx.conf

conf.d/unicorn.conf

NOTE!!!
# path for static files
root /path/to/your_app/current/public;

# Rails error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
  root /path/to/your_app/current/public;
}

Now,go into your application directory. Copy unicorn.rb into config directory.

unicorn.rb

run:

[sudo] unicorn_rails -c /config/unicorn.rb -D -E production

And than restart your nginx server. Finished!

Next let’s set up capistrano for rails3 application.
4)install capistrano
add capistrano in your Gemfile

gem 'capistrano'

Than

[sudo] bundle install

And

capify .
# This will create Capfile under the root and deploy.rb under /config

6)The last thing is create your own deploy.rb file

deploy.rb

Finally

cap deploy

The End.

centos nginx init script

解决在 /etc/init.d/ 下找不到nginx的问题。
原来通过source安装的nginx默认不会新建init脚本,一开始我还以为自己安装出错了,折腾半天。

网上找了篇比较好的文章,自己新建 init 脚本。centos-adding-an-nginx-init-script

copy 如下:

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

注意:自己替换对应的nginx和nginx配置文件路径。