解决 Nginx 部署,无法访问 Swagger 文档的问题

原文链接: https://www.changxuan.top/?p=1161

起因

昨天的时候,同事遇到了一个问题需要访问一个 nginx 部署的项目的 swagger 文档,但是在路由转发规则那里加上了 swagger-ui.html 之后仍不能访问。平时我们在内网开发,可以直接访问 后端项目ip:port/swagger-ui.html 就可以了。但是目前只有 nginx 所在服务器外网可以访问,所以外面的人想看接口文档只能通过 nginx 转发请求。

解决办法

nginx 的其它配置不需要改动,直接将下面的location 配置添加到你的配置中即可:

location ~* ^(/v2|/webjars|/swagger-resources|/swagger-ui.html){
       proxy_set_header Host $host;
       proxy_set_header  X-Real-IP  $remote_addr;
       proxy_set_header X-Forwarded-For $remote_addr;
       #proxy_set_header Host $host:$server_port;
       proxy_set_header X-Forwarded-Proto $scheme;
       proxy_set_header X-Forwarded-Port $server_port;
       proxy_pass http://10.18.66.66:8600; # 后端服务地址
}

其实这也不是什么大问题,只要熟悉 nginx 基本配置都可以很快解决。但是我看网上好多博文都没说到关键的地方,大多数在讲应该如何配置 proxy_set_header主要的原因则是 swagger-ui.html 页面虽然看着只是一个页面,但是当你打开这个页面的时候还伴随着其它的请求。如果在 nginx 的配置中没有处理那些请求相应的转发规则,那么自然不会显示文档页面。

已标记关键词 清除标记
![图片说明](https://img-ask.csdn.net/upload/201812/21/1545383287_910353.png) 相关配置文件如下 ``` user www www; worker_processes auto; error_log /data/wwwlogs/error_nginx.log crit; pid /var/run/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; multi_accept on; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 1024m; client_body_buffer_size 10m; sendfile on; tcp_nopush on; keepalive_timeout 120; server_tokens off; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; fastcgi_intercept_errors on; #Gzip Compression gzip on; gzip_buffers 16 8k; gzip_comp_level 6; gzip_http_version 1.1; gzip_min_length 256; gzip_proxied any; gzip_vary on; gzip_types text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml text/javascript application/javascript application/x-javascript text/x-json application/json application/x-web-app-manifest+json text/css text/plain text/x-component font/opentype application/x-font-ttf application/vnd.ms-fontobject image/x-icon; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; ##If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency. #open_file_cache max=1000 inactive=20s; #open_file_cache_valid 30s; #open_file_cache_min_uses 2; #open_file_cache_errors on; #######################appapi######################### upstream api{ server 119.3.201.144:8080; server 119.3.206.88:8080; } server { listen 7070; access_log /data/wwwlogs/access_nginx.log combined; error_log /data/wwwlogs/error_nginx.log; server_name localhost; location / { proxy_pass http://api/appapi/; include proxy.conf; } } #######################coap######################### server { listen 9090; access_log /data/wwwlogs/access_nginx.log combined; error_log /data/wwwlogs/error_nginx.log; server_name localhost; location / { proxy_pass http://api/coap/; include proxy.conf; } } ########################## vhost ############################# #include vhost/*.conf; } proxy.conf proxy_connect_timeout 300s; proxy_send_timeout 900; proxy_read_timeout 900; proxy_buffer_size 32k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_redirect off; proxy_hide_header Vary; proxy_set_header Accept-Encoding ''; proxy_set_header Referer $http_referer; proxy_set_header Cookie $http_cookie; proxy_set_header Host $host; 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; ~ ```
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页