草庐IT

LNMP第二部分nginx、php配置(用户认证、域名重定向、日志、配置缓存、防盗链)

sxct 2023-03-28 原文
  LNMP第二部分nginx、php配置(用户认证、域名重定向、日志、配置缓存、防盗链)


                  一.nginx的配置( nginx.conf 

1nginx的主配置文件位置: /usr/local/nginx/conf/nginx.con

 

2、清空  /usr/local/nginx/conf/nginx.con默认的配置文件内容

       [root@mysql ~]# > /usr/local/nginx/conf/nginx.conf

   >:重定向的意思,单独使用,可以把一个文本文档快速清空

 

3、拷贝一下代码到/usr/local/nginx/conf/nginx.conf文件中,虚拟主机和主配置文件写在一起

 

主配置文件内容如下

 user nobody nobody;

worker_processes 2;          #开启几个子线程

error_log /usr/local/nginx/logs/nginx_error.log crit; #错误日志级别,crit是非常严谨的级别

pid /usr/local/nginx/logs/nginx.pid;

worker_rlimit_nofile 51200;  #单个进程最大连接数

events

{

    use epoll;

    worker_connections 6000;   #这里的数字不易过大

}

http

 

{

    include mime.types;

    default_type application/octet-stream;

    server_names_hash_bucket_size 3526;   #虚拟主机多,如果这个值设置的很小的话可能会导致无法启动,有三四域名,那么设置成256基本就可以

   server_names_hash_max_size 4096;

    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'

    '$host "$request_uri" $status'

    '"$http_referer" "$http_user_agent"';

    sendfile on;

    tcp_nopush on;

    keepalive_timeout 30;

    client_header_timeout 3m;

    client_body_timeout 3m;

    send_timeout 3m;

    connection_pool_size 256;

    client_header_buffer_size 1k;

    large_client_header_buffers 8 4k;

    request_pool_size 4k;

    output_buffers 4 32k;

   postpone_output 1460;

    client_max_body_size 10m;

    client_body_buffer_size 256k;

    client_body_temp_path /usr/local/nginx/client_body_temp;

    proxy_temp_path /usr/local/nginx/proxy_temp;

    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;

    fastcgi_intercept_errors on;

    tcp_nodelay on;

    gzip on;                                       #是否支持压缩

    gzip_min_length 1k;

    gzip_buffers 4 8k;

    gzip_comp_level 5;

    gzip_http_version 1.1;

    gzip_types text/plain application/x-javascript text/css text/htm application/xml;

   

 

server

 

{

    listen 80;

    server_name localhost;

    index index.html index.htm index.php;

    root /usr/local/nginx/html;

 

    location ~ \.php$ {

        include fastcgi_params;

        fastcgi_pass unix:/tmp/php-fcgi.sock;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;

    }

}

}

 

user nobody nobodyuser后面跟的是nginx运行的用户和组

1-1024端口必须要用超级用户才能监听,普通用户是无法监听的

 

4、也可以使用另外一种写法,就是把虚拟主机的配置文件和主配置文件单独写,而不是写在主配置文件中

     1)主配置文件写法

    

user nobody nobody; worker_processes 2; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 51200; events {     use epoll;     worker_connections 6000; } http   {     include mime.types;     default_type application/octet-stream;     server_names_hash_bucket_size 3526;     server_names_hash_max_size 4096;     log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'     '$host "$request_uri" $status'     '"$http_referer" "$http_user_agent"';     sendfile on;     tcp_nopush on;     keepalive_timeout 30;     client_header_timeout 3m;     client_body_timeout 3m;     send_timeout 3m;     connection_pool_size 256;     client_header_buffer_size 1k;     large_client_header_buffers 8 4k;     request_pool_size 4k;     output_buffers 4 32k;     postpone_output 1460;     client_max_body_size 10m;     client_body_buffer_size 256k;     client_body_temp_path /usr/local/nginx/client_body_temp;     proxy_temp_path /usr/local/nginx/proxy_temp;     fastcgi_temp_path /usr/local/nginx/fastcgi_temp;     fastcgi_intercept_errors on;     tcp_nodelay on;     gzip on;     gzip_min_length 1k;     gzip_buffers 4 8k;     gzip_comp_level 5;     gzip_http_version 1.1;     gzip_types text/plain application/x-javascript text/css text/htm application/xml;     include vhosts/*.conf; #这里也可以写成绝对路径   } 

2)配置默认的虚拟机,这是第一个虚拟机也是默认的虚拟机,就是在没有指定的情况下,都会跳转到这里

    /usr/local/nginx/conf/目录下创建vhosts目录

          [root@mysql ~]# mkdir /usr/local/nginx/conf/vhosts   

    

    进入/usr/local/nginx/conf/vhosts目录

        [root@mysql ~]#cd /usr/local/nginx/conf/vhosts/

 

   在使用vm创建一个default.cong文件,并且拷贝一下代码到default.conf文件中

        [root@mysql vhosts]# vim default.conf

  虚拟机配置文件代码如下:

  

server   {     listen 80;  #在80后面加上default就变成默认的虚拟主机了     server_name localhost;     index index.html index.htm index.php;     root /usr/local/nginx/html;       location ~ \.php$ {         include fastcgi_params;         fastcgi_pass unix:/tmp/php-fcgi.sock; #如果监听的是127.0.0.1:9000的端口,那么这里就要写上127.0.0.1:9000         fastcgi_index index.php;         fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;     } }如图所示:

④然后退出保存

 

⑤检查看看有没有错误

[root@mysql vhosts]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful出现这个提示就表示配置文件没有错误

 

⑥重新加载:[root@mysql vhosts]# /usr/local/nginx/sbin/nginx -s reload

 

⑦如何检查配置文件有没有加载

         需要做的就是故意把/usr/local/nginx/conf/vhosts/配置文件写错,然后使用/usr/local/nginx/sbin/nginx -t检查配置文件看看是否有报错,如果没有,那就说明配置文件没有被加载,如果有说吗配置未见出错,那么就证明配置文件已经被加载

 

3)配置第二个虚拟机

①进入到/usr/local/nginx/conf/vhosts目录下,然后拷贝第一个虚拟主机的配置文件到当前目录下,并且重命名

   [root@mysql vhosts]#cp default.conf default2.conf

修改default2.conf文件,具体如下

代码如下:

server   {     listen 80;   #端口号也是可以修改的         server_name www.guhantai.com.cn www.guhantai.cn;     index index.html index.htm index.php;     root /usr/local/nginx/html;       location ~ \.php$ {         include fastcgi_params;         fastcgi_pass unix:/tmp/php-fcgi.sock;         fastcgi_index index.php;         fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;     } }③退出保存

④检查配置文件是否有错误

[root@mysql vhosts]#/usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful⑤重新加载

[root@mysql vhosts]#/usr/local/nginx/sbin/nginx -s reload

 

⑥测试

  default 表示有一个默认的虚拟机主机

   

 

                                二. php-fpm.conf

1、清空/usr/local/php/etc/php-fpm.conf文件中的内容,然后写入如下配置:

vim   /usr/local/php/etc/php-fpm.conf   

配置文件内容:

 

[global]   #全局配置

pid = /usr/local/php/var/run/php-fpm.pid

error_log = /usr/local/php/var/log/php-fpm.log #定义错误日志位置

[www] 

listen = /tmp/php-fcgi.sock #或者写成127.0.0.1:80也可以

user = php-fpm 

group = php-fpm

listen.owner = nobody  //和后面的nginx的一致

listen.group = nobody // 同上

pm = dynamic 

pm.max_children = 50 

pm.start_servers = 20 

pm.min_spare_servers = 5  #最小的攀升资源数

pm.max_spare_servers = 35 #最大的攀升资源数

pm.max_requests = 500

rlimit_files = 1024 

如图所示:


有多个池子的写法如下图:

listen.owner = nobody  //和后面的nginx的一致,如果不定义,默认会以root的身份去生成这个/tmp/php-fcgi.sock文件,但是他的other是没有读权限的,nginx调用的时候就没有度权限,所以会报502的错误

pm.max_requests = 500  #webphp的请求提供服务的生命周期,500指的是这个进程最多是只能处理500个进程,当500个请求完成之后,就总销毁

rlimit_files = 1024 :文件描述符的数量


2配置多个pool的作用是提供给多个nginx的虚拟主机使用

 

3、配置慢执行日志

     作用: 慢性日志主要是用来进行性能追踪,定位php脚本哪里有问题的

/usr/local/php/etc/php-fpm.conf文件中加入以下两行

      slowlog = /path/to/slow.log #这个位置可以自定义

     request_slowlog_timeout = 1

具体位置如图:

检查配置文件

    [root@mysql ~]# /usr/local/php/sbin/php-fpm -t

   [21-Jun-2015 17:29:30] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful

  这提示表示配置文件时OK

 

定义open_basedir,加入以下一行

   加入这一行:php_admin_value[open_basedir]=/data/www/:/tmp/

如图:

退出保存,检查配置文件

[root@mysql ~]# /usr/local/php/sbin/php-fpm -t

[21-Jun-2015 17:44:40] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful

 

配置文件解释:

     [global]:全局配置

     pid:指定进程id文件

     error_log:指定错误文件存放的位置

     [www]:资源池的名字,有多个资源池时了可以填写数字加以区分

     listen:监听方式,配置要和nginx中的配置一致,有两种方式:

                  第一种:ip+端口的

                  第二种:使用sock文件

    user:启动进程的用户,这里的用户要和启动nginx的一样

    group:启动账户的用户组

    pm = static/dynamic动态、静态子进程

    如果选择static,则由pm.max_children指定固定的子进程数。

 

    如果选择dynamic,则由以下参数决定:

     pm.max_children 子进程最大数

     pm.start_servers 启动时的进程数

     pm.min_spare_servers 保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程

     pm.max_spare_servers 保证空闲进程数最大值,如果空闲进程大于此值,此进行清理

     对于专用服务器,pm可以设置为static。

每一个池子可以单独写一个慢日志,当然日志的存放路径是可以自己定义的

                                          

                                  三. nginx高级配置

 

1. 配置第二个虚拟主机

可以在nginx.conf 加一行(这里跟上面重复了哈)

include  conf/vhosts/*.conf;  

这样,我们就可以在 conf/vhosts目录下创建虚拟主机配置文件了。

vim  conf/vhosts/111.conf   // 加入

server

 

{

    listen 80;

    server_name 111.com;

    index index.html index.htm index.php;

    root /data/www2;

 

    location ~ \.php$ {

        include fastcgi_params;

        fastcgi_pass unix:/tmp/php-fcgi.sock;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /data/www2$fastcgi_script_name;

    }

}

2. 验证默认虚拟主机

listen       80 default_server;

 

3. 用户认证

首先需要安装apache,可以使用yum install httpd 安装

   1) 安装Apache

      yum install httpd

   2)查看Apache安装的目录

       rpm -ql httpd

  3、使用which htpasswd查找htpasswd文件路径

    [root@mysql ~]# which htpasswd

     /usr/bin/htpasswd

 

   4生成密码文件,创建用户

        添加cheng用户,第一次添加时需要加-c参数,第二次添加时不需要-c参数

        [root@mysql ~]# /usr/bin/htpasswd -c /usr/local/nginx/conf/.htpasswd cheng

       New password:

       Re-type new password:

      Adding password for user cheng

 

5在nginx的配置文件中添加 认证方访问方式有两种:

               第一种:针对整个网站的一个认证

location  / {

                      root /data/www/wwwroot/count; #在这里添加root之后,用户访问网站都会要求输入账号和密码,一般我们使用第二种方式

                     auth_basic              "Auth";

                      auth_basic_user_file   /usr/local/nginx/conf/.htpasswd; #用户认证密码和账户存放的位置

            }

 

如图:

保存退出

 

6) 检测配置文件

[root@mysql ~]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

 

7)重启

[root@mysql ~]# /etc/init.d/nginx reload

 

8)在浏览器测试

                    第二种:针对某个目录去限制,而不是整个网站

代码如下:

root /data/www;

 

    location  /w/ {   #这里的w就是用户认证目录,用户访问这个目录下的文件时会要求输入账号和密码来进行认证。

                      auth_basic              "Auth";

                      auth_basic_user_file   /usr/local/nginx/conf/.htpasswd;

            }

具体如图:

 测试:

访问/data/www/1.php文件,如下图所示:

访问/data/www/w/目录下的index2.html文件,提示要输入账号和密码

4. 域名重定向

   1)打开虚拟主机配置文件,我们这里以default2.conf主机为例

     /usr/local/nginx/conf/vhosts/default2.conf文件中加入以下内容:

      如图:

内容:

 

 server_name www.guhantai.com.cn www.guhantai.cn;

    if ($host != 'www.guhantai.cn' ) {

        rewrite  ^/(.*)$  http://www.guhantai.cn/$1  permanent;

    }

退出保存

2)检查配置文件

   

[root@mysql ~]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

 3)重启

[root@mysql ~]# /etc/init.d/nginx reload

重新载入 Nginx:

 

4)测试

   

[root@mysql ~]# curl -I -x127.0.0.1:80 www.guhantai.com.cn

HTTP/1.1 301 Moved Permanently    #显示的状态是301#

Server: nginx/1.6.2

Date: Sun, 21 Jun 2015 20:28:16 GMT

Content-Type: text/html

Content-Length: 184

Connection: keep-alive

Location: http://www.guhantai.cn/  #跳转成功#

 

5.  日志相关

    1日志切割:

     ① 编写脚本:

     vim  /usr/local/sbin/logrotate.sh #/logrotate.sh这个文件默认是没有了,使用vim新建的

加入以下内容:

 

#! /bin/bash

d=`date -d "-1 day" +%Y%m%d`

/bin/mv /home/logs/default2.log /home/logs/default2_$d.log #重命名,移动位置

/etc/init.d/nginx reload >/dev/null 2> /dev/null                 

cd /home/logs   #进入/home/logs/目录

gzip default2_$d.log  #压缩default2_$d.log文件,以便节省空间

 

使用zcat可以查看压缩的文件

 

保存退出

 

②定义日志

    打开/usr/local/nginx/conf/vhosts/default2.conf配置文件

    加上以下内容:

    access_log /home/logs/default2.log combined_realip;

    加在如下图所示的位置:

   

combined_realip:日志的格式,是在nginx.conf文件中定义的,如图:

创建/home/logs/目录

[root@mysql ~]# mkdir /home/logs/

 

检查配置文件

[root@mysql ~]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

 

⑤重新加载

[root@mysql ~]# /usr/local/nginx/sbin/nginx -s reload

 

测试下,看看是否产生的有日志

 [root@mysql ~]#  curl -I -x127.0.0.1:80 www.guhantai.com.cn  #执行了两次

 

⑦查看日志,有两行,因为上面执行了两次,所有有两行

[root@mysql ~]# cat /home/logs/default2.log

127.0.0.1 - [22/Jun/2015:05:36:01 +0800]www.guhantai.com.cn "/" 301"-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"

127.0.0.1 - [22/Jun/2015:05:36:03 +0800]www.guhantai.com.cn "/" 301"-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"

 

⑧执行脚本,在/usr/local/nginx/conf/vhosts目录下

[root@mysql vhosts]# sh -x /usr/local/sbin/logrotate.sh

-x:是为了查看执行过程

 

使用ls查看/home/logs/目录是,会有一下两个文件

[root@mysql vhosts]#ls /home/logs/ default2_20150621.log  default2.log-d "-1 day":指的是昨天的日志

 

2日志格式 

 

log_format main '$remote_addr - $remote_user [$time_local] $request '

                    '"$status" $body_bytes_sent "$http_referer" '

                    '"$http_user_agent" "$http_x_forwarded_for"';

 

 

 

log_format main1 '$proxy_add_x_forwarded_for - $remote_user [$time_local] ' 

                      '"$request" $status $body_bytes_sent '

                      '"$http_referer" "$http_user_agent"';  //此日志格式为,ip不仅记录代理的ip还记录远程客户端真实IP

 

 

3错误日志error_log日志级别 

 

error_log 级别分为 debug, info, notice, warn, error, crit  默认为crit, 该级别在日志名后边定义格式如下:

error_log  /your/path/error.log crit;  

crit 记录的日志最少,而debug记录的日志最多。如果你的nginx遇到一些问题,比如502比较频繁出现,但是看默认的error_log并没有看到有意义的信息,那么就可以调一下错误日志的级别,当你调成error级别时,错误日志记录的内容会更加丰富。

 

日志格式定义的位置在/usr/local/nginx/conf/nginx.conf配置文件中,具体如图:

6. 配置缓存

     是针对某个网站来说做的,所以配置在虚拟主机中,还是以default2.conf虚拟主机为例

    1)编辑/usr/local/nginx/conf/vhosts/default2.conf配置文件,将以下内容拷贝到文件中去

       内容如下:

 

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

        {

                expires      30d;

                access_log off;

        }

 

        location ~ .*\.(js|css)$

        {

                expires      12h;

                access_log off;

        }

 

  location ~ :匹配的意思

(gif|jpg|jpeg|png|bmp|swf):以这些结尾的文件,设定的缓存时间为30

expires:缓存时间

access_log off:不记录日志

如图:

保存退出

2)检查配置文件

 [root@mysql vhosts]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

 

3)重新加载

[root@mysql vhosts]# /usr/local/nginx/sbin/nginx -s reload

 

4)测试

[root@mysql vhosts]# curl -I -x127.0.0.1:80 www.guhantai.cn/1.jpg

HTTP/1.1 200 OK

Server: nginx/1.6.2

Date: Mon, 22 Jun 2015 01:17:52 GMT

Content-Type: p_w_picpath/jpeg

Content-Length: 0

Last-Modified: Mon, 22 Jun 2015 01:16:22 GMT

Connection: keep-alive

ETag: "558761e6-0"

Expires: Wed, 22 Jul 2015 01:17:52 GMT

Cache-Control: max-age=2592000  #这里换算过来正好30天,当然能我也不会算

Accept-Ranges: bytes

 

 

7. 防盗链

1)在虚拟主机重配置,写入一下内容,还是以default2.conf虚拟主机为例

内容如下:

location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ {   

                valid_referers none blocked server_names  *.taobao.com *.baidu.com *.google.com *.google.cn *.soso.com ;  // 对这些域名的网站不进行盗链。

                if ($invalid_referer) {

#                        return 403;

                        rewrite ^/ http://www.example.com/nophoto.gif;

                        }

                }

2如果前面配置中(指的是前面的静态缓存配置)已经加了

                 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

                         {

                                 expires      30d;

                                 access_log off;

                         }

那么会和这一部分重复,就要把前面的注释掉所以结合起来的配置如下

 location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ {

         expires 10d;

         valid_referers none blocked server_names *.guhantai.com.cn *.guhantai.cn *.b.com *.baidu.com\

         *.google.com *.google.cn *.soso.com ;

         if ($invalid_referer) {

              return 403;

              #rewrite ^/ http://www.example.com/nophoto.gif;

         }

         access_log off;

    }

①退出保存

 

②检查配置文件

[root@mysql vhosts]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

 

③重新加载nginx

[root@mysql vhosts]# /usr/local/nginx/sbin/nginx -s reload

 

④测试

不允许盗链测试

[root@mysql vhosts]# curl -x127.0.0.1:80 -e "http://abg.com/345" -I 'http://www.guhantai.cn/w/1.jpg'

HTTP/1.1 403 Forbidden #这里是403

Server: nginx/1.6.2

Date: Mon, 22 Jun 2015 03:12:20 GMT

Content-Type: text/html

Content-Length: 168

Connection: keep-alive

"http://abg.com/345 这个网站是虚拟出来的一个,没有在允许的范围之类,所以报错403的错误

 

允许盗链测试

[root@mysql vhosts]# curl -x127.0.0.1:80 -e "http://www.guhantai.com.cn/345" -I 'http://www.guhantai.cn/w/1.jpg'

HTTP/1.1 404 Not Found   #这里是404,因为我的网站下面根本就没有345这个目录,所以提示为找到

Server: nginx/1.6.2

Date: Mon, 22 Jun 2015 03:25:01 GMT

Content-Type: text/html

Content-Length: 168

Connection: keep-alive

 

⑤查看日志

[root@mysql ~]# less /home/logs/default2.log

 

不指定网站去访问时,是OK

[root@mysql vhosts]# curl -x127.0.0.1:80 -I 'http://www.guhantai.cn/w/123.jpg'

HTTP/1.1 200 OK  #这里200说明是OK

Server: nginx/1.6.2

Date: Mon, 22 Jun 2015 03:39:04 GMT

Content-Type: p_w_picpath/jpeg

Content-Length: 568777

Last-Modified: Mon, 22 Jun 2015 03:38:17 GMT

Connection: keep-alive

ETag: "55878329-8adc9"

Expires: Thu, 02 Jul 2015 03:39:04 GMT

Cache-Control: max-age=864000

Accept-Ranges: bytes


     笔记有错误的地方还请大神指正,小白会继续修改 

有关LNMP第二部分nginx、php配置(用户认证、域名重定向、日志、配置缓存、防盗链)的更多相关文章

  1. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  2. ruby-on-rails - 独立 ruby​​ 脚本的配置文件 - 2

    我有一个在Linux服务器上运行的ruby​​脚本。它不使用rails或任何东西。它基本上是一个命令行ruby​​脚本,可以像这样传递参数:./ruby_script.rbarg1arg2如何将参数抽象到配置文件(例如yaml文件或其他文件)中?您能否举例说明如何做到这一点?提前谢谢你。 最佳答案 首先,您可以运行一个写入YAML配置文件的独立脚本:require"yaml"File.write("path_to_yaml_file",[arg1,arg2].to_yaml)然后,在您的应用中阅读它:require"yaml"arg

  3. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

  4. Ruby Sinatra 配置用于生产和开发 - 2

    我已经在Sinatra上创建了应用程序,它代表了一个简单的API。我想在生产和开发上进行部署。我想在部署时选择,是开发还是生产,一些方法的逻辑应该改变,这取决于部署类型。是否有任何想法,如何完成以及解决此问题的一些示例。例子:我有代码get'/api/test'doreturn"Itisdev"end但是在部署到生产环境之后我想在运行/api/test之后看到ItisPROD如何实现? 最佳答案 根据SinatraDocumentation:EnvironmentscanbesetthroughtheRACK_ENVenvironm

  5. ruby-on-rails - 简单的 Ruby on Rails 问题——如何将评论附加到用户和文章? - 2

    我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。

  6. ruby - RVM "ERROR: Unable to checkout branch ."单用户 - 2

    我在新的Debian6VirtualBoxVM上安装RVM时遇到问题。我已经安装了所有需要的包并使用下载了安装脚本(curl-shttps://rvm.beginrescueend.com/install/rvm)>rvm,但以单个用户身份运行时bashrvm我收到以下错误消息:ERROR:Unabletocheckoutbranch.安装在这里停止,并且(据我所知)没有安装RVM的任何文件。如果我以root身份运行脚本(对于多用户安装),我会收到另一条消息:Successfullycheckedoutbranch''安装程序继续并指示成功,但未添加.rvm目录,甚至在修改我的.bas

  7. ruby - 将 spawn() 的标准输出/标准错误重定向到 Ruby 中的字符串 - 2

    我想使用spawn(针对多个并发子进程)在Ruby中执行一个外部进程,并将标准输出或标准错误收集到一个字符串中,其方式类似于使用Python的子进程Popen.communicate()可以完成的操作。我尝试将:out/:err重定向到一个新的StringIO对象,但这会生成一个ArgumentError,并且临时重新定义$stdxxx会混淆子进程的输出。 最佳答案 如果你不喜欢popen,这是我的方法:r,w=IO.pipepid=Process.spawn(command,:out=>w,:err=>[:child,:out])

  8. Vscode+Cmake配置并运行opencv环境(Windows和Ubuntu大同小异) - 2

    之前在培训新生的时候,windows环境下配置opencv环境一直教的都是网上主流的vsstudio配置属性表,但是这个似乎对新生来说难度略高(虽然个人觉得完全是他们自己的问题),加之暑假之后对cmake实在是爱不释手,且这样配置确实十分简单(其实都不需要配置),故斗胆妄言vscode下配置CV之法。其实极为简单,图比较多所以很长。如果你看此文还配不好,你应该思考一下是不是自己的问题。闲话少说,直接开始。0.CMkae简介有的人到大二了都不知道cmake是什么,我不说是谁。CMake是一个开源免费并且跨平台的构建工具,可以用简单的语句来描述所有平台的编译过程。它能够根据当前所在平台输出对应的m

  9. 神州数码无线产品(AC+AP)配置 - 2

    注意:本文主要掌握DCN自研无线产品的基本配置方法和注意事项,能够进行一般的项目实施、调试与运维AP基本配置命令AP登录用户名和密码均为:adminAP默认IP地址为:192.168.1.10AP默认情况下DHCP开启AP静态地址配置:setmanagementstatic-ip192.168.10.1AP开启/关闭DHCP功能:setmanagementdhcp-statusup/downAP设置默认网关:setstatic-ip-routegeteway192.168.10.254查看AP基本信息:getsystemgetmanagementgetmanaged-apgetrouteAP配

  10. hadoop安装之保姆级教程(二)之YARN的配置 - 2

    1.1.1 YARN的介绍 为克服Hadoop1.0中HDFS和MapReduce存在的各种问题⽽提出的,针对Hadoop1.0中的MapReduce在扩展性和多框架⽀持⽅⾯的不⾜,提出了全新的资源管理框架YARN. ApacheYARN(YetanotherResourceNegotiator的缩写)是Hadoop集群的资源管理系统,负责为计算程序提供服务器计算资源,相当于⼀个分布式的操作系统平台,⽽MapReduce等计算程序则相当于运⾏于操作系统之上的应⽤程序。 YARN被引⼊Hadoop2,最初是为了改善MapReduce的实现,但是因为具有⾜够的通⽤性,同样可以⽀持其他的分布式计算模

随机推荐