王筝的博客
ruby学习

(1)首先在current文件夹下新建服务有关的nginx配置文件nginx.conf

upstream news_server {
  server unix:/tmp/unicorn.news.sock fail_timeout=0;
}

server {
  listen 8888;
  access_log /opt/app/ruby/news/current/log/nginx.access.log;
  error_log /opt/app/ruby/news/current/log/nginx.error.log;

  client_max_body_size 4G; 

  keepalive_timeout 5;

  root /opt/app/ruby/news/current/public;

  location ^~ /assets/ {
    gzip on; 
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_redirect  off;
    proxy_set_header  Host  $http_host;

    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_pass http://news_server;
  }
  error_page 500 502 503 504 /500.html;
  location = /500.html {
    root /opt/app/ruby/news/current/public;
  }
}

(2)在服务器检查下nginx语法,顺便查看位置

[root@localhost opt]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

查看nginx的配置

vim /etc/nginx/nginx.conf

找到默认conf配置文件放的位置

user  nginx;
worker_processes  8;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

从最后一句话中我们可以得知,所有/etc/nginx/conf.d/下的后缀名为.conf的配置文件都会被include进来,

所以需要做一个软链接,将我们应用部署目录下的nginx配置文件链到这个文件夹下,重启检测下语法 并重启nginx

[root@localhost opt]# ln -s /opt/app/ruby/news/current/config/nginx.conf /etc/nginx/conf.d/new.conf
[root@localhost opt]# ll /etc/nginx/conf.d/
总用量 8
-rw-r--r-- 1 root root 1097 9月  16 2014 default.conf
-rw-r--r-- 1 root root  427 9月  16 2014 example_ssl.conf
lrwxrwxrwx 1 root root   44 9月  18 10:56 new.conf -> /opt/app/ruby/news/current/config/nginx.conf
[root@localhost opt]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost opt]# nginx -s reload
[root@localhost opt]# netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:xxxx            0.0.0.0:*               LISTEN      8591/nginx: worker  
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8591/nginx: worker  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      2656/sshd           
tcp        0      0 0.0.0.0:xxxx            0.0.0.0:*               LISTEN      8591/nginx: worker  
tcp        0      0 0.0.0.0:xxxx            0.0.0.0:*               LISTEN      14200/python        
tcp6       0      0 :::3306                 :::*                    LISTEN      15326/mysqld        
tcp6       0      0 :::21                   :::*                    LISTEN      5186/vsftpd         
tcp6       0      0 :::22                   :::*                    LISTEN      2656/sshd

 

 

@73 @74
13:4* 73352 24470
13:5* 120485 40336
13:** 511953 170672

@73
14:12 [logwatch@u]$ cat /opt/logs/nginx/access/log | grep “24/Sep/2014:13:5″ | grep blacklist | wc -l
73352
14:12 [logwatch@u]$ cat /opt/logs/nginx/access/log | grep “24/Sep/2014:13:4″ | grep blacklist | wc -l
120485
14:12 [logwatch@u]$ cat /opt/logs/nginx/access/log | grep “24/Sep/2014:13″ | grep blacklist | wc -l
511953

@74
14:11 [logwatch@y]$ cat /opt/logs/nginx/access/log | grep “24/Sep/2014:13:5″ | grep blacklist | wc -l
24470
tty:[0] jobs:[0] cwd:[~]
14:11 [logwatch@y]$ cat /opt/logs/nginx/access/log | grep “24/Sep/2014:13:4″ | grep blacklist | wc -l
40336
tty:[0] jobs:[0] cwd:[~]
14:12 [logwatch@y]$ cat /opt/logs/nginx/access/log | grep “24/Sep/2014:13″ | grep blacklist | wc -l
170672

在大师的指导下配置了新的服务器的nginx,通过top命令查看了服务器是8个cpu的,所以起了8个端口,把它们都映射到一个总的端口3600上,需要在做如下配置。1. nginx 中做如下配置:

server {
         listen 3600;
         charset utf-8;
         location / {
             proxy_pass          http://aaa_servers;
             proxy_redirect      default;
             proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header    X-Real-IP $remote_addr;
             proxy_set_header    Host $http_host;
             proxy_next_upstream http_502 http_504 error timeout invalid_header;
        }
     }
     upstream aaa_servers{
            server 127.0.0.1:3201;
            server 127.0.0.1:3202;
            server 127.0.0.1:3203;
            server 127.0.0.1:3204;
            server 127.0.0.1:3205;
            server 127.0.0.1:3206;
            server 127.0.0.1:3207;
            server 127.0.0.1:3208;}

重启 nginx:

  $ nginx -t  (测试一下配置文件)
  $ nginx -s reload

2. 使用 配置文件来启动 thin:

2.1. 建立 /config/thin.yml , 内容如下:

---
chdir: '/opt/app/ruby/mmm/current  #  这里需要修改。
environment: production
address: 0.0.0.0
port: 3201   (这是第一个端口的端口号码)
timeout: 30
log: log/thin.log
pid: tmp/pids/thin.pid
max_conns: 1024
max_persistent_conns: 512
require: []
wait: 30
servers: 8     (这里确定启动几个端口)
daemonize: true

2.2. 启动thin: (记得Gemfile 中要有 gem ‘thin’ )

    $ bundle exec thin restart -C config/thin.yml

2.3. 在 config/environments/production.rb文件中: (以后可以使用nginx来 配置,处理静态文件)

Cms::Application.configure do
    config.serve_static_assets = true
end

就可以了。

$cap -T

$cap deploy:setup

$cap deploy           (遇到报错说没有config文件,所以先去服务器端新建个)

$ssh 10.10.10.10  -l root -p 33333   (地址及端口号纯属虚构)

$mkdir config      (在shared目录)

$rbenv rehash       (current 目录)

$ ruby -v              (报错:ruby:command not found,不过显示了version)

$rbenv global 1.9.3-p327 (这种错误不是每次都出现,在部署出错的时候出现)

$ruby -v               (成功执行了)

$bundle exec thin start -C config/thin.yml  (在current里,报错thin没在Gemfile)

这里把本地和服务器的Gemfile都修改下,加入 gem ‘thin’, ‘1.6.1’

$bundle

这里可以把一些之前别的发布文件里的config里的配置文件拷贝过来,thin.yml里面的内容需要做修改,比如第二行的文件目录chdir,第五行的端口号port

现在可以到/opt/nginx/conf/nginx.conf里新增一下端口和路由的配置

$/opt/nginx/sbin/nginx -t   在sbin文件里测试下

$/opt/nginx/sbin/nginx -s reload   在nginx文件夹里

$bundle exec thin start -C config/thin.yml

$ps -ef |grep 5218     查看端口是否启动