2 ways to solve the Web API site under Nginx Load swagger cannot be used

Source: Internet
Author: User

Web API interface site, introduced swagger to generate online API documents in real time, but also facilitates the online testing of API interface. Swagger:the World's most Popular Framework for APIs.

There is no problem with local testing. Publishing to production, the problem arises. --the site deployed on the line is a load of 3 nodes made with Nginx. Nginx is configured with a public domain name and mapped to a site on 3 node IIS. 3 single-node port is not the default 80, this problem arises: when access to swagger, swagger automatically get the URL of the document contains this port, because the site is exposed to only the domain name, with the port can not access, so the swagger document is not available .

Of course, manually removing the port from this explorer is a list of interfaces that can be displayed.

Similar problems also occur when an interface is called by the impersonation, because the port is displayed at the same time, the request cannot be sent to the server, resulting in an error response. See:

Since the swagger code is integrated into a DLL, the project file does not have any static scripts, pictures, style files swagger, we can not modify its source code.

I have tried to refer to this question in the CSDN forum http://bbs.csdn.net/topics/391964196, the more I get is the criticism of the unknown.

Yesterday, it occurred to me whether we could intercept the HTTP response and then modify its response (that is, remove the port in the response).

By trying to pass the endrequest event in the Global.ascx.cx of the program site, it won't work.

Next, called to the development Group of 2 students, explain the situation, a classmate said can intercept HTTP request to fix.

The next day, he really realized. The 2 JS files from swagger are inferred from the Chrome browser Debug tool, which affects these 2 places. Then we get the 2 JS code from chrome, modify the code which gets the URL of the website (if it is the production environment domain name, then replace the port), and then save the JS file in the Scripts folder of the website file, Finally in the GLOBAL.ASCX.CX do request interception processing.

protected voidApplication_BeginRequest (Objectsender, EventArgs e) {    if(Request.Url.ToString (). Contains ("Marked-js") {Response.Redirect ("/scripts/swagger-oauth.js"); }    Else if(Request.Url.ToString (). Contains ("Swagger-ui-min-js") {Response.Redirect ("/scripts/swagger-ui-min.js"); }}

I also have a solution, since the browser debugging tool in the network can see the invocation of JS, then, we put the above method in the modified JS file, generate a copy of its reference path the same directory structure, put in the site file. You can do it.

At this point, it is important to note that the Swagger UI refers to the swagger JS does not have an extension, and no extension of the file in the browser is not allowed to access by default, so you need to add MIME type, In IIS settings or in the Web. config add-in configuration (for files with no suffix, the extension is required to fill in ".") Symbol, refer to http://www.zhaomu.com/news/detail-393.html).

<configuration>    <system.webServer>        <staticContent>            <mimemap fileextension=  ". " mimetype="text/javascript "/>        </staticContent>    </ System.webserver></configuration>

2 ways to solve the Web API site under Nginx Load swagger cannot be used

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.