如何让Rails Admin导航标签链接以HTTPS开头?
Got it, let's tackle this issue where Rails Admin's left nav links are still using HTTP even though your site is fully set up for HTTPS. The root cause is usually that Rails (and by extension Rails Admin) isn't correctly detecting the HTTPS protocol from your Nginx proxy, or there's a missing configuration to enforce HTTPS in Rails Admin's URL generation.
Here are the step-by-step fixes to try, ordered by how likely they are to resolve your problem:
1. Ensure Nginx Passes the HTTPS Protocol Header to Rails
First, make sure your Nginx configuration is telling Rails that the original client request was over HTTPS. Add these headers to your Nginx location block that proxies traffic to Rails:
location / { # Your existing proxy settings (like proxy_pass) go here... proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $host; }
This sends critical context about the request protocol to Rails, so it knows it's handling an HTTPS connection behind the proxy.
2. Configure Rails to Trust Proxy Headers
Next, update your Rails environment config (e.g., config/environments/production.rb) to trust the headers sent by Nginx, and explicitly set the default URL protocol:
# Trust your Nginx server's IP address (adjust if your proxy is on a separate server) config.action_dispatch.trusted_proxies = ['127.0.0.1', '::1'] # Tie your HOST env variable to Rails' default URL settings, with HTTPS enforced config.action_controller.default_url_options = { host: ENV['HOST'], protocol: 'https' } # Optional: Force all site-wide requests to use HTTPS (use this if you want full-site SSL) config.force_ssl = true
The trusted_proxies line is crucial—without it, Rails will ignore the X-Forwarded-Proto header and default to HTTP for URL generation.
3. Explicitly Enforce HTTPS in Rails Admin's Config
If the above steps don't fix the nav links, you can directly override Rails Admin's URL generation behavior. Add this to your config/initializers/rails_admin.rb file:
RailsAdmin.config do |config| # Your existing Rails Admin configuration (like authentication, models, etc.) goes here... # Force all Rails Admin-generated URLs to use HTTPS config.default_url_options = { protocol: 'https' } end
This bypasses any auto-detection quirks and ensures every absolute link in Rails Admin uses the HTTPS protocol.
4. Double-Check Your HOST Environment Variable
Verify that your application.yml has the correct domain (without the http:// or https:// prefix):
HOST: "your-production-domain.com"
Rails will append the HTTPS protocol we configured earlier, so including the prefix here would cause issues.
After making these changes, restart your Rails server and Nginx. If you have any cached assets or Rails cache, clear those too—sometimes old cached URLs can stick around. Your Rails Admin navigation links should now show up with https:// instead of http://.
内容的提问来源于stack exchange,提问作者Allen

