反应:404在部署到IIS后找不到代理

发布于 2025-01-22 01:29:55 字数 1405 浏览 6 评论 0原文

我真的需要您的帮助和建议,我已经搜索了2周,而我无法解决我的代理问题。
我正在设置以下' setUpproxy.js ',以绕过 cors ,同时请求(Web服务器)的API/端点。 但是,使用终端运行终端时,代理和原点替换仅适用于Localhost 3000。由于某些原因,部署到IIS后它不起作用,它不断获得“ 404 ”。您能告诉我在这里解决问题吗?

setupproxy.js:

app.use('/XXX.Order.API',
    createProxyMiddleware({
        target:'https://gcm.dellsvc',
        secure: false,
        changeOrigin: true                 
    })
);  

axios呼叫:

let XXX =
      "/XXX.Order.API/api/v1/orders/sample"

终端/localhost:3000结果:( 200 ok ) 但是,在IIS浏览中:8080(http):(找不到404 )

web.config文件:

<?xml version="1.0"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
     <rule name="Rewrite Text Requests" stopProcessing="true">
         <match url=".*" />
             <conditions>
                        <add input="{HTTP_METHOD}" pattern="^GET$" />
                        <add input="{HTTP_ACCEPT}" pattern="^text/html" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
             </conditions>
             <action type="Rewrite" url="/index.html" />
     </rule>
</rules>
    </rewrite>
  </system.webServer>
</configuration>

I really need your help and advice on this, I have been searching for 2 weeks and I'am unable to resolve my proxy issues.
I'm setting up the below 'setupProxy.js' to bypass the CORS while requesting the API/Endpoints from the (Web Servers).
However, the proxy and Origin replacement only works from localhost 3000 when using the terminal to run it. For some reasons, It doesn't work after deploying to IIS, it keep getting "404 Not Found". Could you please tell me what should I do to fix the issue here?

setupProxy.js:

app.use('/XXX.Order.API',
    createProxyMiddleware({
        target:'https://gcm.dellsvc',
        secure: false,
        changeOrigin: true                 
    })
);  

Axios call:

let XXX =
      "/XXX.Order.API/api/v1/orders/sample"

Terminal/Localhost:3000 result: (200 ok)
However, in IIS browse :8080 (http) : (404 Not Found)

web.config file:

<?xml version="1.0"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
     <rule name="Rewrite Text Requests" stopProcessing="true">
         <match url=".*" />
             <conditions>
                        <add input="{HTTP_METHOD}" pattern="^GET
quot; />
                        <add input="{HTTP_ACCEPT}" pattern="^text/html" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
             </conditions>
             <action type="Rewrite" url="/index.html" />
     </rule>
</rules>
    </rewrite>
  </system.webServer>
</configuration>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情域 2025-01-29 01:29:55

我遇到了两个星期的问题。最后,我有一个令人满意的解决方案。
包装中的HTTP-Proxy-Middleware或代理。JSON仅用于开发目的(我认为)。
您创建的是绝对正确的,但是您只能在开发中使用它。
对于生产设置,仅在URL重写级别上处理。我认为这是合乎逻辑的。它与代码是分开的,如果代码中存在错误(例如指向Localhost的端点不好),则不会影响生产。

解决方案是修改在IIS中牢固存储的Web.config文件中的URL重写。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Reverse Proxy to api" stopProcessing="true">
          <match url="^api/(.*)" />
          <action type="Rewrite" url="https://api.example.com/{R:0}" />
        </rule>
        <rule name="Reverse Proxy to pusher" stopProcessing="true">
          <match url="^pusher/(.*)" />
          <action type="Rewrite" url="https://pusher.example.com/{R:0}" />
        </rule>
        <rule name="React Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

i had exactly the same problem i was dealing with for 2 weeks. Finally I have a satisfactory solution.
http-proxy-middleware or proxy in package.json is intended (in my opinion) for development purposes only.
What you have created is absolutely correct, but you can only use it in development.
For production settings, is only handled at the url rewrite level. And I think it's logical. It's separate from the code, and if there's an error in the code (eg a bad endpoint pointing to localhost), it won't affect production.

The solution is to modify the url rewrite in the web.config file, which is stored securely in IIS.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Reverse Proxy to api" stopProcessing="true">
          <match url="^api/(.*)" />
          <action type="Rewrite" url="https://api.example.com/{R:0}" />
        </rule>
        <rule name="Reverse Proxy to pusher" stopProcessing="true">
          <match url="^pusher/(.*)" />
          <action type="Rewrite" url="https://pusher.example.com/{R:0}" />
        </rule>
        <rule name="React Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文