跳到主内容
Rust
文章阅读

docker部署salvo和后台前端代码

2025/12/16116 次阅读4 分钟

1. 编写nginx.conf配置文件


events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen 5802;
        server_name 0.0.0.0;


        # API 代理配置
        location /api {
            proxy_pass http://0.0.0.0:5800;  # 转发到目标地址
            proxy_set_header Host $host;             # 转发请求的 Host 头
            proxy_set_header X-Real-IP $remote_addr; # 客户端真实 IP
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 转发链 IP
        }



        location / {
            root /usr/share/nginx/html;
            index index.html;
            try_files $uri /index.html;
        }



                # 直接提供静态资源
        location /static/ {
             root /usr/share/nginx/html;
        }
    }
    gzip  on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 9;
    gzip_types       text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php application/javascript application/json;
    gzip_disable "MSIE [1-6]\.";
    gzip_vary on;
}

要调整的参数有

名称描述
listen 5802自定义nginx监听端口
proxy_pass http://0.0.0.0:5800salvo服务地址
root /usr/share/nginx/htmlhtml静态文件路径

2. 编写dockerfile

把savlo的一些配置和数据库拷贝进镜像,还有静态html文件


FROM alpine:latest

RUN echo "http://mirrors.aliyun.com/alpine/latest-stable/main/" > /etc/apk/repositories && \
    echo "http://mirrors.aliyun.com/alpine/latest-stable/community/" >> /etc/apk/repositories

ENV TZ Asia/Shanghai
RUN apk add tzdata && cp /usr/share/zoneinfo/${TZ} /etc/localtime \
    && echo ${TZ} > /etc/timezone \
    && apk del tzdata \
    && apk update && apk add --no-cache nginx \
    && apk add curl

RUN mkdir -p /run/nginx \
    && mkdir -p /usr/share/nginx/html

COPY ./admin-antd/nginx.conf /etc/nginx/nginx.conf
COPY ./admin-antd/dist /usr/share/nginx/html

WORKDIR /home/app-dir
COPY ./target/x86_64-unknown-linux-musl/release/d_blog ./d_blog
COPY ./data ./data
COPY ./config ./config
COPY ./assets ./assets
COPY ./logs ./logs
ENV LANG en US.UTF-8
ENV LANGUAGE en US:en
ENV LC ALL en US.UTF-8
EXPOSE 5800 5801 5802
ENTRYPOINT ["sh", "-c","nginx -g 'daemon off;' & /home/app-dir/d_blog"]


entrypoint 要先启动nginx,rust可执行文件后面再运行

EXPOSE 5800 5801 5802 开放端口

返回顶部