Nginx

已解決:nginx 上的 rails 應用程序 + apache2 mod_proxy 後面的乘客,ajax 上傳失敗並出現錯誤 500

  • May 11, 2017

情況如下:我將一個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 webmaster@design-union.co.uk
   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 + 乘客上執行應用程序,它確實會正確上傳文件,但在其他 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/ 文件夾,但隨後不會移動到應用程序 system/files/ 文件夾。我雖然可能是權限,但為了測試,我 chmod 了完整的項目文件和目錄 777,但沒有運氣。nginx 和 apache 程序都從使用者 vu2029 執行。

這是在埠 3000 上執行的為同一應用程序提供服務的獨立乘客的日誌:

   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]'

我正在通過命令行檢查並且“辨識命令已安裝在伺服器上,所有 imagemagick 軟體包都已安裝。訪問文件的權限,或者回形針模組無法訪問辨識文件?

已解決:最後,它是 Paperclip gem 的路徑配置。我在 config/environments/production.rb 文件中添加了以下內容:

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

引用自:https://serverfault.com/questions/849100