Nginx Docker容器上的静态网页缺少CSS

时间:2016-10-27 09:13:00

标签: css nginx docker swagger

我正在尝试使用Nginx在Docker容器上托管Swagger UI。

当我通过hostAddress.com访问我的网页时,它将网页作为纯文本返回并检查它告诉我它无法找到任何javascript或css文件,尽管它们似乎存在于容器中因为我有ssh进入容器检查。

我的dockerfile

FROM nginx
COPY src /usr/share/nginx/html
COPY config/nginx.conf /etc/nginx
EXPOSE 80

nginx.config

events {
  worker_connections  4096;  ## Default: 1024
}

http {
  server {
listen       80;
server_name  localhost;
root   /usr/share/nginx/html;
index  index.html index.htm;
include /etc/nginx/mime.types;


  location /swagger {
  try_files $uri /index.html;
}

#Static File Caching. All static files with the following extension will be cached for 1 day
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
  expires 1d;
    }
  }
}

1 个答案:

答案 0 :(得分:0)

你可以做到这一点。

就像提到评论一样,你可以从中下载文物 https://github.com/ianneub/docker-swagger-ui/blob/master

  • 创建目录说ngix
  • Dockerfilerun.sh复制到该目录
  • 修改Dockerfile以进行更改位置所需的自定义,如下所示:
FROM nginx:1.9

ENV SWAGGER_UI_VERSION 2.1.2-M2
ENV URL **None**

RUN apt-get update \
    && apt-get install -y curl \
    && curl -L https://github.com/swagger-api/swagger-ui/archive/v${SWAGGER_UI_VERSION}.tar.gz | tar -zxv -C /tmp \
    && mkdir /usr/share/nginx/html/swagger \
    && cp -R /tmp/swagger-ui-${SWAGGER_UI_VERSION}/dist/* /usr/share/nginx/html/swagger \
    && rm -rf /tmp/*

COPY run.sh /run.sh

CMD ["/run.sh"]
  • 如果您将上述更改与原始Dockerfile进行比较,则会在第9,10行中进行更改,以包含其他路径,即swagger。当然,您可以根据需要进行更改。

接下来,运行以下docker命令来构建并运行

构建图片:

docker build -t myswagger .

运行

docker run -it --rm -p 3000:80 --name testmyswagger -e "URL=http://petstore.swagger.io/v2/swagger.json" myswagger

现在,您应该可以使用http://localhost:3000/swagger/index.html

访问招摇了