新手问题 在项目中引入 Swagger 线上访问不到

hopenofool · 2020年11月17日 · 最后由 hopenofool 回复于 2020年11月21日 · 1919 次阅读

这个问题纠结了很久,不知道哪里出了问题,没有找到解决方法,还望社区能提供思路,谢谢啦。

最开始在项目 gemfile 中引入了 gem 'swagger_ui_engine',访问http://localhost:3000/api_docs/swagger_docs/v1#/正常访问

到线上却访问不到

线上查看 docker 日志:

看日志是需要访问/usr/share/nginx/html/api_docs/swagger_docs/v1 下的文件,我于是把 swagger.yaml 放在了这个路径,还是不能访问。

在本地有 rails s -e production 试过,也是可以访问。

是还需要修改什么吗?

routes:

mount SwaggerUiEngine::Engine, at: "/api_docs"
mount Sidekiq::Web => '/admin/sidekiq'
mount ExceptionTrack::Engine => "/admin/exception-track"

这三个页面都遇到了这个问题,其他 routes 里的请求都是正常的。

再次感谢!

swagger 的内容是动态生成的,你看下 nginx 静态目录设置吧,大概是这个原因。要不贴上 nginx 配置 让彭于晏们看看

yingce 回复

nginx.config

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        client_max_body_size 100M;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

/etc/nginx/conf.d/guiqie.conf

server {
        listen       80;
        server_name  guiqie.cc;

        location / {
            #add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, PUT, OPTIONS';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,token';
            proxy_set_header    Host $host;
            proxy_set_header    X-Real-IP $remote_addr;
            proxy_pass http://127.0.0.1:7000;
            proxy_http_version  1.1;
        }
}

麻烦大家了😭

感觉请求到 nginx 的默认服务器配置里了,没到 /etc/nginx/conf.d/guiqie.conf 这个配置文件,可以 grep -r "/usr/share/nginx/html" /etc/nginx 看到是哪个站点配置文件用了这个 document root。

5 楼 已删除
tranch_xiao 回复

include /etc/nginx/sites-enabled/*;

感觉可能是这一行引入的配置覆盖了你的配置。nginx 推荐的配置放置方式是先在 /etc/nginx/sites-available 下创建配置文件,然后再把实际需要使用的配置文件软链接到 /etc/nginx/sites-enabled 下面,比如:

ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/

我猜测你可能是在它前面插入了 include /etc/nginx/conf.d/*.conf; 这行配置,后面这行可能覆盖了你前面导入的配置。

tranch_xiao 回复

输入grep -r "/usr/share/nginx/html" /etc/nginx没有,然后改成了grep -r "/usr/share" /etc/nginx

结果如下: /etc/nginx/sites-available/default:# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.

tranch_xiao 回复

嗯嗯,conf.d/guiqie.conf 应该是走了,因为里面配置了域名和域名指向的 ip:port,在 postman 发送请求都是正常的。

/etc/nginx/sites-available现在是软连接到/etc/nginx/sites-enabled下的。

然后我在nginx.conf我改成了

include /etc/nginx/sites-enabled/*;
include /etc/nginx/conf.d/*.conf;

发现还是不行,是我哪里可能还有问题吗?

/etc/nginx/sites-enabled 下的 default 删掉试试

tranch_xiao 回复

已删除,重启 nginx,再访问好像还是不行

看下现在日志报什么错?

tranch_xiao 回复
2020/11/18 07:11:41 [error] 42#42: *6509 open() "/usr/share/nginx/html/api_docs/swagger_docs/v1" failed (2: No such file or directory)

nginx 中加一个跳转,ip 换成对应 docker 的 ip 和端口试试

location /admin/sidekiq {
        proxy_pass http://127.0.0.1:3000/admin/sidekiq
    }
w569893882 回复

嗯,我拿 exception-track 试下,我在 nginx 加上了

location /admin/exception-track {
  proxy_pass http://127.0.0.1:7000/admin/exception-track;
}

然后重启了,不行😅

你重启 nginx 前最好确认配置文件语法没问题 nginx -t 测试一下。没问题了再 reload 一下 nginx -s reload。如果配置文件语法有问题 其实不会重启的。
配置文件上我没看出来问题 感觉像是你的配置文件根本没生效
另外 proxy_pass 默认用不用跟路径

location /admin/exception-track {
  proxy_pass http://127.0.0.1:7000;
}
yingce 回复

嗯嗯,已修改 proxy_pass,拼接的路径就是http://127.0.0.1:7000/admin/exception-track

但是还是访问不到😅

还有一种比较骚气的可能是配置都是对的,但是浏览器缓存的原因导致跳转到旧的

w569893882 回复

似乎不是,清理了缓存😂

hopenofool 回复

你 curl 访问 rails 服务器试试呢,我没看到代码暂时看 nginx 配置是没发现什么问题了

w569893882 回复

他这个是 nginx 报的路径错误 大概率不是缓存问题

yingce 回复

是 curl 接口吗,curl 接口的话都是正常的,我 postman 请求的话也是正常,不明白为什么路由中加入了 mount 的这几个地址不行

已找大佬远程帮忙解决,是 docker 内部还有个 nginx,把请求拦截了,谢谢大家。

hopenofool 关闭了讨论。 11月21日 14:14
需要 登录 后方可回复, 如果你还没有账号请 注册新账号