【问题标题】:how to use angular and apache on same nginx with same domain如何在具有相同域的相同 nginx 上使用 angular 和 apache
【发布时间】:2020-01-26 12:27:11
【问题描述】:

TLDR:配置nginx监听80端口并使用

  • 如果/api,则在localhost:8081 上表达JS
  • 如果/blog,wordpress/apache2 在localhost:8082
  • 其他角度服务器localhost:8080 angular 和 express 服务器正常工作,但需要配置 wordpress/apache。

详细一点, 我在同一个 VPS 上安装了以下内容:

  1. angular 端口8080 上的前端
  2. expressJS 端口8081 上的后端
  3. WordPress 在端口 8082 上的 apache2 上运行

目前,我正在使用 nginx 来操作请求,并且配置是

server {
        listen 443;

        ssl      on;
        ssl_certificate         /home/user/.ssh/ssl/ssl.crt;
        ssl_certificate_key     /home/user/.ssh/ssl/ssl.key.txt;

        root /var/www/html;

        index index.html index.htm index.nginx-debian.html index.php;

        server_name _;

        location / {
                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;
            proxy_pass          http://localhost:8080;
            proxy_read_timeout  90;
        }

        location /api {
                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;
            proxy_pass          http://localhost:8081;
            proxy_read_timeout  90;
        }
}

现在我需要在mysite.com/blog 上托管我的博客。所以我在 nginx 配置中添加了以下内容。

 location /blog {
                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;
            proxy_pass          http://localhost:8082;
            proxy_read_timeout  90;
        }

现在当我转到example.com/blog 时,它会重定向到example.com:8082/blog。如何同时配置nginx和apache?

PS:只要 wordpress 工作,Apache 就不是必需的。但我喜欢保留 nginx

PPS:我已将 nginx 配置为通过重定向 301 将所有 http 请求转发到 https://example.com/

【问题讨论】:

    标签: php angular wordpress apache nginx


    【解决方案1】:

    您需要在 proxy_pass 参数的末尾添加一个额外的 / 以使其成为完整的 URI(而不仅仅是协议/主机名/端口组合),以便 Nginx 负责 URL 重写:

    location /blog/ {
      ...
      proxy_pass http://localhost:8082/;
      ...
    }
    

    然后它将删除请求 URL 的 /blog 前缀,并将剩余部分附加到 http://localhost:8082/ 根 URL。

    来自Nginx documentation

    一个请求 URI 被传递给服务器,如下所示:

    如果使用 URI 指定 proxy_pass 指令,那么当 请求被传递到服务器,规范化请求 URI 的一部分 匹配位置被指令中指定的 URI 替换:

    location /name/ { proxy_pass http://127.0.0.1/remote/; }

    如果 proxy_pass 没有指定 URI,则请求 URI 以相同的方式传递给服务器 处理原始请求时客户端发送的表单,或 处理更改时传递完整的规范化请求 URI 网址:

    location /some/path/ { proxy_pass http://127.0.0.1; }

    或者,您可以使用 rewritedirective 手动强制 URL 重写:

    location /blog/ {
      rewrite    /blog/?(.*) /$1 break;
      proxy_pass http://localhost:8082;
    }
    

    这应该可以解决您当前的问题,但是如果您想完全摆脱 Apache HTTP 服务器和反向代理指令,您可以查看PHP FastCGI 以直接从 Nginx 提供 Wordpress/PHP。

    【讨论】:

    • 我添加了/,但结果相同。在不同的设备和隐身模式下测试。
    • @SalithaIndrajithPathiraja 尝试在/blog 位置的末尾添加一个/,就像我在编辑后的答案中所做的那样。
    • 不,先生,结果还是一样。
    • @SalithaIndrajithPathiraja 您尝试访问的完整 URL 是什么?
    • @SalithaIndrajithPathiraja 尝试“example.com/blog/”看看它是否有效,否则重新检查您的配置文件并确保其正确重新加载。如果仍然失败,请尝试手动 URL 重写替代方法。
    猜你喜欢
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 2015-08-13
    相关资源
    最近更新 更多