Programing

[GET] / 자산과 일치하는 경로가 없습니다

crosscheck 2020. 6. 25. 08:15
반응형

[GET] / 자산과 일치하는 경로가 없습니다


프로덕션 환경에서 테스트하려는 Rails 앱이 있습니다. 나는 실행 RAILS_ENV=production rake assets:precompile에 / 공공 / 자산 내 모든 자산을 생성한다. 문제는 내가 앱을 시작할 때 RAILS_ENV=production rails s thin얻는 것입니다.

ActionController::RoutingError (No route matches [GET] "/assets/application-eff78fd67423795a7be3aa21512f0bd2.css"):

이 파일은에 있습니다 /public/assets/application-eff78fd67423795a7be3aa21512f0bd2.css.

내가 왜 이것을 얻는 지에 대한 생각이 RoutingError있습니까?


프로덕션 모드에서 Rails는 정적 자산 서비스를 담당하지 않습니다. 따라서이 오류가 발생합니다. Rails의 래퍼 일 뿐이므로 Thin도 그렇게하지 않을 것입니다.

이것은 config/environments/production.rb응용 프로그램 의이 설정에 의해 제어됩니다 .

config.serve_static_files = false

또는 Rails 5에서 :

# config/environments/production.rb
config.public_file_server.enabled = true

또는 ENV['RAILS_SERVE_STATIC_FILES']true로 설정하십시오 .

이를 설정 true하거나 정적 자산을 제공하는 Apache 또는 Nginx와 같은 실제 서버를 사용할 수 있습니다 . 포로도 그렇게 할 수있을 것 같아요.


Heroku를 사용하는 경우 rails_12factor기본적 으로이 설정을 활성화 하는 보석을 사용 하는 것이 좋습니다 . production다음 Gemfile과 같이 보석을 그룹에 배치하십시오 .

group :production do
  gem 'rails_12factor'
end

Ryan이 위에서 말한 내용에 덧붙여 Rails 자산 파이프 라인 안내서는 정적 자산을 제공하도록 Apache 또는 nginx를 설정하는 방법을 설명합니다.

http://guides.rubyonrails.org/asset_pipeline.html

mongrel / thin / unicorn보다이 작업에 훨씬 더 최적화되어 정적 자산을 제공하도록 nginx 또는 Apache를 설정해야합니다.


같은 문제를 해결했습니다. 제 경우에는 Ryan의 대답이 도움이되지 않았습니다. Bratsche는 Rails Guides를 지적했지만 불행히도 이것은 나에게도 효과가 없었습니다. 그러나 리소스가 도움이되었습니다. 그래서 Nginx 구성을 가져 와서 루트 디렉토리를 추가하여 공용 디렉토리를 가리 켰습니다. 이것이 없으면 작동하지 않습니다.

   # serve static assets
   location ~ ^/assets/ {
     expires 1y;
     root  /path/to/my/cool_project/public;
     add_header Cache-Control public;

     add_header ETag "";
     break;
   }

nginx를 다시 시작하면됩니다.


실제로 기본 구성을 수정할 필요가 없습니다. 당신은 단지 자산 파일을 다시 컴파일합니다 .

공개 / 자산 제거

1. 레이크 자산 : 클로버 RAILS_ENV = 생산

자산 컴파일

2. 레이크 자산 : 사전 컴파일 RAILS_ENV = 생산

3. 다시 시작 서버, 예 (nginx)


Rails 4.2는 config / environments / staging.rb 및 production.rb 파일에서이 줄을 추가 / 변경했습니다 :

config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

RAILS_SERVE_STATIC_FILES가 설정되어 있지 않고 Unicorn과 같이 Rails 서버의 서비스 자산 인 경우 기본적으로 "false"가되고 RoutingError가 발생합니다.

이것은 쉬운 수정입니다.

config.serve_static_files = true

레일 5에서 config.serve_static_files옵션이 변경되었으므로 이제

config.public_file_server.enabled = true

자산을 로컬로 제공합니다.


아래 코드를보십시오 :

config / environments / production.rb

config.assets.compile = true

그런 다음 명령을 실행하십시오.

RAILS_ENV=production rake assets:precompile

그런 다음 모든 컴파일 파일과 매니페스트 파일을 서버로 푸시하십시오.


내가 사용 미나 + 푸마 + 의 nginx를 내가 가진 내 레일 5 응용 프로그램을 배포하는

ActionController::RoutingError (No route matches [GET] "/assets/application-658cf2ab3ac93aa5cb41a762b52cf49d7184509c307922cd3fbb61b237a59c1a.css")

config / environments / production.rb를 확인하십시오

# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

NGINX는 이미 이것을 처리 하고 있습니다.

upstream puma {
  server unix:///home/deploy/apps/appname/shared/tmp/sockets/appname-puma.sock;
}

server {
  listen 80 default_server deferred;
  # server_name example.com;

  root /home/deploy/apps/appname/current/public;
  access_log /home/deploy/apps/appname/current/log/nginx.access.log;
  error_log /home/deploy/apps/appname/current/log/nginx.error.log info;

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

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;
}

일이 잘 될 것입니다.


누군가 내가 테스트 환경에서와 같은 오류로 여기에 오면 여기에 도움이 된 것이 있습니다.

rails assets:clobber assets:precompile RAILS_ENV=test

그때:

ps axu | grep your-username

to find spring server process and his PID then kill it via:

kill <spring-server-PID>

참고URL : https://stackoverflow.com/questions/7829480/no-route-matches-get-assets

반응형