【问题标题】:How to redirect in Multiple pages within in nginx on clicking a button如何在单击按钮时在 nginx 中的多个页面中重定向
【发布时间】:2021-06-24 02:18:50
【问题描述】:

我有一个使用 TypeScript 创建的包含多个页面(登录、注册、忘记密码等)的 Web 应用程序。 我想创建这样的配置文件,当用户点击登录页面上的提交按钮时,它应该将他们带到主页,或者如果他们点击忘记密码,它应该将他们带到忘记密码页面。我如何在 nginx 中实现这个场景。

我进行了研究,但找不到相关文档或博客。

附上我的示例 nginx.conf 供您参考。这个 Conf 文件只有所有这些页面的位置配置。

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  _;
        root   html/webconsole/WebConsole/src;

        location / {
            index  index.html index.htm;
        }
        location /register {
            alias  html/webconsole/WebConsole/src/app/register/;
            index register.component.html register.component.htm;
        }
        location /register-confirm {
            alias  html/webconsole/WebConsole/src/app/register-confirm/;
            index register-confirm.component.html register-confirm.component.htm;
        }
        location /login {
            alias  html/webconsole/WebConsole/src/app/login/;
            index login.component.html login.component.htm;
        }
        location /forget-pwd {
            alias  html/webconsole/WebConsole/src/app/forget-pwd/;
            index forget-pwd.component.html forget-pwd.component.htm;
        }
        location /resend {
            alias  html/webconsole/WebConsole/src/app/resend/;
            index resend.component.html resend.component.htm;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

让我知道如何才能达到我的要求。

提前致谢

【问题讨论】:

  • 我不明白nginx和你的问题有什么关系。所有这些重定向都应该在您的 Web 应用源代码中完成。您可以使用简单的超链接并将其设置为按钮样式,也可以直接使用 JavaScript(或 TypeScript)进行 - 请参阅 How do I redirect to another webpage?
  • 嗨@IvanShatsky 谢谢你的回复。我已经有 TS 文件可以满足这个要求,但是我如何在 nginx 中定义它应该读取相应的 ts 文件?

标签: typescript nginx


【解决方案1】:

我居然找到了答案!因为我正在使用 Angular 应用程序。我需要安装 node.js & npm & 需要运行 ng build 来获取我的代码包 & 使用该包我可以为 nginx 提供服务

后续步骤:

  1. 安装 Nodejs&npm
  2. 为节点包执行 npm 安装
  3. 在我的源目录中安装 Angular cli 并运行 ng build 命令

【讨论】:

    【解决方案2】:

    你可以使用try_files指令:

    root html/webconsole/WebConsole/src;
    location / {
        index index.html index.htm;
        try_files $uri $uri/ /app$uri$uri.component.html /app$uri$uri.component.htm =404;
    }
    

    try_files 指令的每个参数,除了最后一个被视为 URI 的参数。因此对于http://example.com/register 请求,nginx 将采用/register 部分并检查以下文件是否存在(请记住,$uri 变量等于/register):

    • 为第一个参数$uri-html/webconsole/WebConsole/src/register文件;
    • 对于第二个参数$uri/ - html/webconsole/WebConsole/src/register/index.htmlhtml/webconsole/WebConsole/src/register/index.htm 文件(您被声明为索引文件的文件);
    • 对于第三个参数/app$uri$uri.component.html(每次出现$uri 都替换为/register)-html/webconsole/WebConsole/src/app/register/register.component.html 文件;
    • 最后是html/webconsole/WebConsole/src/app/register/register.component.htm 文件。

    如果以上文件都没有找到,nginx会返回HTTP 404 Not found,由第五个=404参数指定。

    还有一件事要提到。每一次这样的检查都会导致stat UNIX 系统调用。为了稍微减少服务器工作量,我建议删除未使用的文件扩展名 - 仅保留 .html.htm 变体,具体取决于您的实际文件扩展名。例如,

    root html/webconsole/WebConsole/src;
    location / {
        index index.html;
        try_files $uri $uri/ /app$uri$uri.component.html =404;
    }
    

    此配置将产生不超过 3 个stat 系统调用(与第一个配置版本的 5 个stat 调用相比)。

    【讨论】:

    • 嗨@ivan 我试过这个,我能够查看登录页面&当我点击登录按钮时它的重定向但给出 405 错误,当我研究它是由于 Nginx 不支持 POST 请求然后我尝试使用此命令 #error_page 405 =200 $uri;此命令没有给出 405 错误,但它停留在同一页面中
    • 是的,这是一种已知的方式,使用 error_page 指令将 POST 请求重写为 GET。但是这样我们就丢失了请求正文。看起来您的应用使用带有 POST 提交操作的 HTML 表单。无法从客户端 JavaScript 访问 POST 请求正文。看起来您正在尝试以完全错误的方式托管您的应用程序。我想它应该在 node.js 控制下运行,这是我知道的 JS/TS 应用程序能够访问 POST 正文数据的唯一方法。你可以查看this的文章。
    • 通常使用 nginx 作为 node.js 项目的反向代理/HTTPS 终结器,请参阅Node.js + Nginx - What now? 线程。无论如何,如果我是对的,您将需要使用 proxy_pass 指令,而这个答案对您毫无用处。
    猜你喜欢
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 2021-10-24
    • 1970-01-01
    • 2017-01-15
    相关资源
    最近更新 更多