我有一个包含 importmap、scss、rails 7 和 nginx 的 Rails 项目。
我使用 digital ocean 上的 vps 来通过 docker 和 nginx 进行部署。
我的 Rails 代码留在这里 https://github.com/TryTech/try_tech
和我的 nginx.conf
worker_processes auto;
worker_rlimit_nofile 500000;
pcre_jit on;
error_log /var/log/nginx/error.log warn;
events {
use epoll;
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
access_log off;
error_log /dev/null emerg;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
upstream web {
server localhost:3000;
server localhost:3001;
keepalive 500;
}
server {
listen 443 ssl;
server_name trytech.app;
ssl_certificate /var/www/certbot/live/trytech.app/fullchain.pem;
ssl_certificate_key /var/www/certbot/live/trytech.app/privkey.pem;
location / {
proxy_buffering off;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Keep-Alive "";
proxy_set_header Proxy-Connection "keep-alive";
proxy_pass http://web;
}
location ~ ^/assets/ {
expires 1y;
add_header Cache-Control public;
add_header ETag "";
}
}
}
我使用 docker-compose 来启动容器。
docker-compose:
version: '3.8'
services:
certbot:
image: certbot/certbot:latest
volumes:
- ./certbot/www/:/var/www/certbot/:rw
- ./certbot/conf/:/etc/letsencrypt/:rw
nginx:
image: "docker.io/nginx:${NGINX_VERSION}"
container_name: nginx
command: ["nginx", "-g", "daemon off;"]
#entrypoint: 'tail -f /dev/null'
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./mime.types:/etc/nginx/mime.types
- ./certbot/conf/:/var/www/certbot/:ro
# - public_rails:/rails/public
depends_on:
- web1
- web2
ulimits:
nproc: 1000000
nofile:
soft: 1000000
hard: 1000000
network_mode: host
deploy:
resources:
limits:
cpus: '0.15'
memory: '0.3GB'
web1: &web
image: "ghcr.io/trytech/try_tech:release"
container_name: web1
# volumes:
# - public_rails:/rails/public
env_file:
- ./.env.prod
environment:
PORT: 3000
depends_on:
- postgres
- redis
network_mode: host
hostname: web1
deploy:
resources:
limits:
cpus: '0.45'
memory: '0.5GB'
web2:
<<: *web
hostname: web2
container_name: web2
environment:
PORT: 3001
redis:
container_name: redis
image: "docker.io/redis:${REDIS_VERSION}"
command: redis-server --save "" --appendonly no --maxclients 20000
hostname: redis
network_mode: host
deploy:
resources:
limits:
cpus: '0.05'
memory: '0.1GB'
postgres:
container_name: postgres
image: "docker.io/postgres:${POSTGRES_VERSION}"
network_mode: host
hostname: postgres
volumes:
- ./postgres/production.sql:/docker-entrypoint-initdb.d/production.sql
- ./postgresql.conf:/docker-entrypoint-initdb.d/postgresql.conf
- postgres_data:/var/lib/postgresql/data
env_file:
- ./.env.prod
healthcheck:
test: [ "CMD-SHELL", "pg_isready" ]
interval: 5s
timeout: 5s
retries: 20
start_period: 10s
deploy:
resources:
limits:
cpus: '0.35'
memory: '1.3GB'
volumes:
postgres_data:
# public_rails:
我认为如果您需要更多信息,这已经足够了。简而言之,我的问题归结为无法使用来自服务器的 javascript 和 css。
我已经尝试过几种实现方式,例如让rails处理资产或仅让nginx处理,但我还没有成功,我对这两种技术都不是很有经验,这是我的第一个项目。如果您想在生产环境中调试浏览器,请使用以下 url。 https://trytech.app/
提前感谢您的宝贵时间
期望网站能够正确使用 javascript 和 css。
您的问题与部署无关。首先在本地修复您的应用程序。
手动添加引导引脚以从 jspm 加载:
# config/importmap.rb
pin "bootstrap", to: "https://ga.jspm.io/npm:[email protected]/dist/js/bootstrap.esm.js"
pin "@popperjs/core", to: "https://ga.jspm.io/npm:@popperjs/[email protected]/lib/index.js"
暂时将
importmap-rails
降级为 v1
:
# Gemfile
gem "importmap-rails", "~> 1.0"
由于此更改,importmap-rails
v2
目前很多时候无法使用: