带Nginx反向代理的Swagger UI

g6ll5ycj  于 2023-03-29  发布在  Nginx
关注(0)|答案(1)|浏览(324)

所以我一直在尝试将Swagger设置为在/swagger/上工作,直到我们切换到一个域为止,它一直工作得很好。我对nginx没有经验,而且它是反向代理。如果有人可以帮助,我将分享我的设置。它实际上打开了页面,但它都是白色的,/redoc/(Redis)也是如此。
Nginx.conf

events {
worker_connections 768;
multi_accept       on;
}

http {

large_client_header_buffers 16 5120k;
fastcgi_read_timeout 900;
proxy_read_timeout 900;   
proxy_connect_timeout 900;
proxy_send_timeout 900;    

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

proxy_buffer_size   128k;
proxy_buffers   4 256k;
proxy_busy_buffers_size   256k;

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

ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;

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

gzip on;

proxy_http_version      1.1;
proxy_set_header        Upgrade $http_upgrade;
proxy_set_header        Connection "upgrade";
proxy_set_header        X-Real-IP $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header        X-Forwarded-Proto $scheme;
proxy_set_header        Host $http_host;
proxy_intercept_errors  on;

server {

    client_max_body_size 250M;
    listen 80;
    server_name mysite.com;

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

        location / {
            return 301 https://$host$request_uri;
    }
}

server {

    client_max_body_size 250M;
    listen 443 ssl;
    server_name mysite.com;

    ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass http://mysite:3000/;
        proxy_redirect off;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;
        proxy_set_header        Host $http_host;
        proxy_intercept_errors  on;
    }

    location /ht/ {
        proxy_pass http://mysite:8000/ht/;
        proxy_redirect off;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;
        proxy_set_header        Host $http_host;
        proxy_intercept_errors  on;
    }

    location /swagger/ {
        proxy_pass http://mysite:8000/swagger/;
        proxy_redirect off;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;
        proxy_set_header        Host $http_host;
        proxy_intercept_errors  on;
    }

例如,/ht/(健康检查)工作正常
urls.py :

urlpatterns = [ 
url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema- 
swagger-ui')]

请告诉我,如果你需要任何其他文件,我愿意分享他们,我只是取代敏感数据与虚拟数据一样mysite.com
先谢了!
编辑:我刚刚注意到在网络选项卡中,它为所有CSS,JS文件显示“Javascript需要启用才能运行此应用程序”,/redoc也是如此,并且控制台日志也抛出了这个:“未捕获的语法错误:JS文件的意外标记“|”。
我忘了说这是一个React应用程序,但应用程序本身运行良好。后端是Python/Django

3zwtqj6y

3zwtqj6y1#

这可能不是你正在寻找的,但我有一个重定向为 swagger 的方式。

location /swagger {
  return 301 http://127.0.0.1:8000/swagger-ui.html#!/;
}

它只是让“入口”的网址更漂亮,但一旦重定向就不会保持“漂亮的形式”。
例如:* http://127.0.0.1/swagger *
将用户移动到:http://127.0.0.1:8000/swagger-ui/index. html #!/
我也想不出怎么做一个反向代理来像你这样 swagger 的。

相关问题