Flask应用部署在IIS上无法处理URL中的Unicode。

huangapple go评论56阅读模式
英文:

Flask app deployed on IIS fails to handle unicode in URL

问题

@app.route("/api/<value>")
def foo(value):
    ...

在我的本地机器上,使用Flask开发服务器,一切都正常。

但是在生产环境中,应用程序部署在IIS上,使用wfastcgi,当name包含非ASCII字符时,我会收到404错误。在IIS日志中,路由看起来正常(在日志文件中显示为UTF-8),但在Flask端,请求以乱码字符接收。以下方法:

@app.before_request
def log_request():
    req: flask.Request = request
    logger.info(
        f"{req.method} {req.path}"
    )

会在日志中打印出以下内容:

GET /api/

这不仅仅是显示问题。值本身是不正确的,我无法使用它来正确处理请求。

我尝试使用urllib包中的quoteunquote方法,但似乎没有解决问题。

是否有任何IIS设置可以解决这个问题?

(IIS 10,Flask 1.1.2)


<details>
<summary>英文:</summary>

I have a Flask app with a route defined like this:

```python
@app.route(&quot;/api/&lt;value&gt;&quot;)
def foo(value):
    ...

On my local machine, using Flask development server, everything works correctly.

But on production, where the app is deployed on IIS using wfastcgi, I get a 404 error when name contains non-ascii characters. In IIS logs the route seems OK (shown on the log file as UTF-8) but on the Flask side the request is received with gibberish characters. The following method:

@app.before_request
def log_request():
    req: flask.Request = request
    logger.info(
        f&quot;{req.method} {req.path}}&quot;
    )

prints this to the log:

GET /api/�

This is more than just a display issue. The value itself is incorrect and I can't use it to process the request correctly.

I tried to use the quote and unquote methods from the urllib package but it does not seem to solve the problem.

Is there any setting on IIS I can use to fix this?

(IIS 10, Flask 1.1.2)

答案1

得分: 0

我在Microsoft关于PHP与FastCGI的文档中找到了以下内容。对于Flask/wfastcgi,这也适用于我:

默认情况下,FastCGI扩展在设置PHP使用的服务器变量时使用ASCII编码。当请求的URL包含非ASCII字符时,从请求的URL字符串派生其值的服务器变量可能被设置不正确。依赖这些服务器变量的PHP应用可能因此而无法正常工作。

为了防止这种情况发生,可以配置FastCGI扩展在设置服务器变量时使用UTF-8编码。要为特定一组服务器变量配置FastCGI以使用UTF-8编码,可以使用REG_MULTI_SZ注册表键FastCGIUtf8ServerVariables,并将其值设置为服务器变量名称的列表。例如:

reg add HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\w3svc\Parameters /v FastCGIUtf8ServerVariables /t REG_MULTI_SZ /d REQUEST_URI\0PATH_INFO
上面的示例配置了FastCGI扩展在设置REQUEST_URI和PATH_INFO服务器变量时使用UTF-8编码。

在设置了注册表键之后,使用iisreset命令重新启动IIS。

英文:

I found the following on Microsoft's documentation for PHP with FastCGI. I worked for me with Flask/wfastcgi as well:

> By default, the FastCGI extension uses ASCII encoding when setting server variables that are used by PHP. When the requested URL contains non-ASCII characters, server variables that derive their values from the requested URL string may be set incorrectly. PHP applications that rely on those server variables may not work as a result.

>To prevent this, the FastCGI extension can be configured to use UTF-8 encoding when setting server variables. To configure FastCGI to use UTF-8 encoding for a particular set of server variables, use the REG_MULTI_SZ registry key FastCGIUtf8ServerVariables and set its value to a list of server variable names. For example:

>reg add HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\w3svc\Parameters /v FastCGIUtf8ServerVariables /t REG_MULTI_SZ /d REQUEST_URI\0PATH_INFO
>The above example configures the FastCGI extension to use UTF-8 encoding when setting the REQUEST_URI and PATH_INFO server variables.

>After setting the registry key, restart IIS by using the iisreset command.

huangapple
  • 本文由 发表于 2023年6月8日 15:44:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定