已解决:nginx 上的 rails 应用程序 + apache2 mod_proxy 后面的 Passenger,ajax 上传失败,错误 500

已解决:nginx 上的 rails 应用程序 + apache2 mod_proxy 后面的 Passenger,ajax 上传失败,错误 500

情况如下:我将 rails 应用程序从具有 nginx + Passenger 的 Ubuntu 服务器迁移到具有 apache2 + Passenger 的 debian 服务器。

一切都很顺利,直到我们测试了一些使用 handlebars.js 和其他脚本加载模板的 ajax 函数。它抱怨 javascript 调用,所以我安装了一个在端口 8080 上服务的 nginx 虚拟主机。然后在 apache 虚拟主机上进行了 apache 代理配置,以将流量重定向到在端口 8080 上监听的 nginx 服务器。原始服务器与 nginx 配合良好,所以我认为这应该可以工作,而且确实如此。

但最近我们测试了应用程序的更多菜单,发现它无法上传文件,尽管如果我从虚拟主机配置中删除代理行,这在 apache2 上可以正常工作。

这是 Apache2 虚拟主机文件:

<VirtualHost 217.13.81.93:443>
    ServerAdmin [email protected]
    ServerName design-union.co.uk
    ServerAlias www.design-union.co.uk vu2029.admin.tantosoft.com

    LogLevel error
    ErrorLog /var/log/apache2/design-union.co.uk/error.log

    #DocumentRoot /var/www/virtual/design-union.co.uk/htdocs
    DocumentRoot /var/www/virtual/design-union.co.uk/htdocs/current.new/public

    PassengerLogLevel 3

    Alias /errors /var/www/virtual/design-union.co.uk/errors/

    SuexecUserGroup vu2029 vu2029

    #SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1

    <FilesMatch ".+\.ph(p[3457]?|t|tml)$">
        SetHandler "proxy:unix:/var/run/php5-fpm-design-union.co.uk.sock|fcgi://design-union.co.uk/"
    </FilesMatch>

    <Directory /var/www/virtual/design-union.co.uk>
        Options +SymLinksIfOwnerMatch
        Require all granted
    </Directory>

    <Directory /var/www/virtual/design-union.co.uk/htdocs>
        AllowOverride All
        Options -MultiViews
     </Directory>

    #ScriptAlias /cgi-bin/ /var/www/virtual/design-union.co.uk/cgi-bin/

    <Directory /var/www/virtual/design-union.co.uk/cgi-bin>
    #    AllowOverride AuthConfig Indexes Limit
    </Directory>

    RedirectMatch permanent ^(/(?:ftp|pma|webmail)[/]?)$ http://admin.tantosoft.com$1
    ProxyRequests Off
    ProxyPass /stats http://localhost/stats/design-union.co.uk
    ProxyPassReverse /stats http://localhost/stats/design-union.co.uk

    ProxyPass / http://www.design-union.co.uk:4443/
    ProxyPassReverse / http://www.design-union.co.uk:4443/

    <Location /stats>
        RewriteEngine on
        RewriteRule ^(.+)?config=([^?&]+)(.*) $1?config=design-union.co.uk&$3 [NC,L]
        AuthType Basic
        AuthName "Statistics for domain design-union.co.uk"
        AuthUserFile /var/www/virtual/design-union.co.uk/.htpasswd
        AuthGroupFile /var/www/virtual/design-union.co.uk/.htgroup
        Require group statistics
    </Location>

    SSLEngine On
    SSLCertificateFile /var/www/imscp/gui/data/certs/design-union.co.uk.pem
    SSLCertificateChainFile /var/www/imscp/gui/data/certs/design-union.co.uk.pem

    Include /etc/apache2/imscp/design-union.co.uk.conf
</VirtualHost>

这是 nginx 虚拟主机文件:

server {
    server_name www.design-union.co.uk;

    listen 217.13.81.93:4443;
    listen [::]:4443;

    ssl on;
    ssl_certificate /var/www/imscp/gui/data/certs/design-union.co.uk.pem;
    ssl_certificate_key /var/www/imscp/gui/data/certs/design-union.co.uk.pem;

    root /var/www/virtual/design-union.co.uk/htdocs/current.new/public;
    passenger_enabled on;
    passenger_min_instances 1;
    passenger_friendly_error_pages on;
    passenger_user vu2029;

    rails_env production;

    client_body_buffer_size 10M;

    location /errors {
        alias /var/www/imscp/gui/public/errordocs;
    }

    error_page 401 /errors/401.html;
    error_page 403 /errors/403.html;
    error_page 404 /errors/404.html;
    error_page 497 https://$host:$server_port$request_uri;
    error_page 500 /errors/500.html;
    error_page 503 /errors/503.html;

    location ~ ^/assets/ {
      gzip_static on;
      expires 1y;
      add_header Cache-Control public;
      add_header ETag "";
      break;
    }

    error_page   500 502 503 504  /server-500.html;
    location /server-500.html {
      root /var/www/nginx-default;
    }
}
passenger_pre_start http://www.design-union.co.uk;

两个虚拟主机定义都使用用户 vu2029 来执行文件系统操作,并且我也将 nginx 用户更改为以 vu2029 身份运行。

如果我删除 Apache2 代理行并在 apache2 + Passenger 上运行应用程序,它确实可以正确上传文件,但其他 javascript 部分会失败。如果我添加代理行并在 nginx 上运行应用程序,它会正确呈现所有 javascript,但无法上传文件。

我快疯了。另请参阅来自 rails production.log 的一些日志:

Started POST "/admin/assets.json" for 88.5.145.53 at 2017-05-09 19:02:09 +0200
Processing by Admin::AssetsController#create as JSON
  Parameters: {"authenticity_token"=>"7iBXhXjcL/Jp5AmeXpT9HV2B/Ilfz08z5CUf1EWkQA=", "file"=>#<ActionDispatch::Http::UploadedFile:0x007f5d58f47c68 @original_filename="walle_back.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"walle_back.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<Tempfile:/tmp/RackMultipart20170509-18136-1o9vefd>>}
  MOPED: 127.0.0.1:27017 QUERY        database=design_union_production collection=admins selector={"$query"=>{"_id"=>"54298de59acc936174000004"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.3929ms)
500: /admin/assets.json :: {"authenticity_token"=>"7iBXhXjcL/Jp5AmeXpT9HV2B/Ilfz08z5CUf1EWkQA=", "file"=>#<ActionDispatch::Http::UploadedFile:0x007f5d58f47c68 @original_filename="walle_back.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"walle_back.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<Tempfile:/tmp/RackMultipart20170509-18136-1o9vefd>>, "action"=>"create", "controller"=>"admin/assets", "format"=>"json"}
  MOPED: 127.0.0.1:27017 QUERY        database=design_union_production collection=pages selector={"$query"=>{"role"=>"error"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.8137ms)
Page.ordered_slices (0.5ms)
  Rendered text template (0.0ms)
Completed 500 Internal Server Error in 75.8ms (Views: 0.9ms)

Firebug控制台日志:

POST https://www.design-union.co.uk:4443/admin/assets.json

500 Internal Server Error
        472ms   
jquery.min.js (línea 4)
EncabezadosPostCookies
ver fuente
Cache-Control   
no-cache
Connection  
keep-alive
Content-Type    
application/json; charset=utf-8
Date    
Tue, 09 May 2017 17:02:09 GMT
Server  
nginx/1.10.2 + Phusion Passenger 5.1.2
Transfer-Encoding   
chunked
X-Powered-By    
Phusion Passenger 5.1.2
X-Rack-Cache    
invalidate, pass
X-Request-Id    
6e4b5a7447ed78d9b3cc24f33f2a30f8
X-Runtime   
0.102857
X-UA-Compatible 
IE=Edge,chrome=1
status  
500 Internal Server Error
ver fuente
Accept  
*/*
Accept-Encoding 
gzip, deflate, br
Accept-Language 
en-US,en;q=0.5
Content-Length  
586541
Content-Type    
multipart/form-data; boundary=---------------------------10471913232033507427250099373
Cookie  
_design_union_session=Bh7CEkiD3Nlc3Npb25faWQGOgZFVEkiJTMzZmUxYTliM2NmYjNlOWZmOGU4NTg3Y2RhMWUwYTcwBj
sAVEkiGndhcmRlbi51c2VyLmFkbWluLmtleQY7AFRbB1sGVToaTW9wZWQ6OkJTT046Ok9iamVjdElkIhFUKY3lmsyTYXQAAARJIi
IkMmEkMTAkWkhpZ1VLMmxtdGFXMms5cG16Ui8wZQY7AFRJIhBfY3NyZl90b2tlbgY7AEZJIjE3eGlCWGhYamNML0pwNUFtZVhwVDlIVjJCL0lsZnowOHo1Q1VmMUVXa1FBPQY7AEY
%3D--11e2341939add257da345ddb03c4bac0d661b3d7; __utma=1.1509736449.1494261374.1494332932.1494339392.4
; __utmc=1; __utmz=1.1494261374.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=85681676.479307049
.1494264038.1494264038.1494333556.2; __utmc=85681676; __utmz=85681676.1494264038.1.1.utmcsr=(direct)
|utmccn=(direct)|utmcmd=(none); __utmb=1.60.10.1494339392; __utmt=1
Host    
www.design-union.co.uk:4443
Referer 
https://www.design-union.co.uk:4443/admin/assets
User-Agent  
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0
X-CSRF-Token    
7iBXhXjcL/Jp5AmeXpT9HV2B/Ilfz08z5CUf1EWkQA=
X-Requested-With    
XMLHttpRequest
"NetworkError: 500 Internal Server Error - https://www.design-union.co.uk:4443/admin/assets.json"
assets.json

文件已上传到服务器的 /tmp/ 文件夹中,但并未移动到应用程序系统/files/ 文件夹中。我以为可能是权限问题,为了测试,我将整个项目文件和目录 chmod 为 777,但没有成功。nginx 和 apache 进程均以用户 vu2029 的身份运行。

这是在端口 3000 上运行、为同一应用程序提供服务的独立 Passenger 的日志:

    Started POST "/admin/assets.json" for 88.5.145.53 at 2017-05-11 12:25:17 +0200
Processing by Admin::AssetsController#create as JSON
  Parameters: {"authenticity_token"=>"7iBXhXjcL/Jp5AmeXpT9HV2B/Ilfz08z5CUf1EWkQA=", "file"=>#<ActionDispatch::Http::UploadedFile:0x0000000680f650 @original_filename="ios_mosaic_hor.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"ios_mosaic_hor.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<Tempfile:/tmp/RackMultipart20170511-15343-13plto5>>}
  MOPED: 127.0.0.1:27017 QUERY        database=design_union_production collection=admins selector={"$query"=>{"_id"=>"54298de59acc936174000004"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.4151ms)
App 15343 stdout: Command :: identify -format %wx%h '/tmp/ios_mosaic_hor20170511-15343-18f5uzb.jpg[0]'
App 15343 stdout: Command :: identify -format %m '/tmp/ios_mosaic_hor20170511-15343-18f5uzb.jpg[0]'
App 15343 stdout: Command :: identify -format %m '/tmp/ios_mosaic_hor20170511-15343-18f5uzb.jpg[0]'
App 15343 stdout: Command :: convert '/tmp/ios_mosaic_hor20170511-15343-18f5uzb.jpg[0]' -resize "256x256>" -strip -density 72x72 -depth 8 '/tmp/ios_mosaic_hor20170511-15343-18f5uzb20170511-15343-1jwvw6t'
App 15343 stdout: Command :: file -b --mime '/tmp/ios_mosaic_hor20170511-15343-18f5uzb20170511-15343-1jwvw6t'
App 15343 stdout: Command :: identify -format %wx%h '/tmp/ios_mosaic_hor20170511-15343-18f5uzb.jpg[0]'
App 15343 stdout: Command :: identify -format %wx%h '/tmp/ios_mosaic_hor20170511-15343-18f5uzb20170511-15343-1jwvw6t20170511-15343-1gl5n8l[0]'
  MOPED: 127.0.0.1:27017 INSERT       database=design_union_production collection=assets documents=[{"_id"=>"59143c0d21232f27e7000002", "file_dimensions"=>{"original"=>"1130x960", "admin"=>"256x217"}, "page_cache"=>[], "file_file_name"=>"ios_mosaic_hor.jpg", "file_content_type"=>"image/jpeg", "file_file_size"=>194168, "file_fingerprint"=>"4fbef76166cb06ae99e9c240e4a189f3", "file_updated_at"=>2017-05-11 10:25:17 UTC, "tags"=>"", "_keywords"=>["ios", "mosaic", "hor", "jpg"], "updated_at"=>2017-05-11 10:25:17 UTC, "created_at"=>2017-05-11 10:25:17 UTC}] flags=[] (0.2253ms)
App 15343 stdout: [paperclip] Saving attachments.
500: /admin/assets.json :: {"authenticity_token"=>"7iBXhXjcL/Jp5AmeXpT9HV2B/Ilfz08z5CUf1EWkQA=", "file"=>#<ActionDispatch::Http::UploadedFile:0x0000000680f650 @original_filename="ios_mosaic_hor.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"ios_mosaic_hor.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<Tempfile:/tmp/RackMultipart20170511-15343-13plto5>>, "action"=>"create", "controller"=>"admin/assets", "format"=>"json"}
  MOPED: 127.0.0.1:27017 QUERY        database=design_union_production collection=pages selector={"$query"=>{"role"=>"error"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.4716ms)
Page.ordered_slices (0.3ms)
  Rendered text template (0.0ms)
Completed 500 Internal Server Error in 99.7ms (Views: 0.6ms)

文件已上传到 /tmp 文件夹,但似乎未移动到应用程序文件文件夹。在 Apache2 中运行应用程序时,一切正常。我卡住了。有什么帮助吗?

编辑:

/var/log/nginx/error.log:

App 26490 stdout: Command :: identify -format %wx%h '/tmp/walle_back20170511-26490-1ka42xa.png[0]'
App 26490 stdout: [paperclip] An error was received while processing: #<Paperclip::Errors::CommandNotFoundError: Could not run the `identify` command. Please install ImageMagick.>
App 26490 stdout: Command :: identify -format %wx%h '/tmp/walle_back20170511-26490-1ka42xa.png[0]'

我正在通过命令行检查,“identify 命令已安装在服务器上,所有 imagemagick 包都已安装。是否有访问文件的权限,或者回形针模块无法访问识别文件?

答案1

已解决:最后,它是 Paperclip gem 的路径配置。我在 config/environments/production.rb 文件中添加了以下内容:

Paperclip.options[:command_path] = "/usr/bin"

相关内容