【问题标题】:How to rewrite path to my server?如何重写我的服务器的路径?
【发布时间】:2016-01-18 05:31:59
【问题描述】:

我正在使用 nginx 将子域添加到我的主域。问题是我的其他服务器索引的代理是 myip/folder/index.php 我在我的添加主机文件中制定了重写规则,当我尝试访问 subdomain.mydomain.com 时,它会重定向到 myip/folder/index.php,但索引的完整路径是可见的。如何在我的添加主机文件中隐藏路径? 我的添加主机文件如下所示:

server {
listen 80;
server_name subdomain.mydomain.com;
location / {
    proxy_pass http://192.168.0.0/;
rewrite ^/$ http://subdomain.mydomain.com/folder/index.php# permanent;
}

}

【问题讨论】:

  • 看来您的location 参数/ 是主要问题。您想用匹配所有的通配符替换斜杠。您使用的语法旨在发送默认文件,如 index.php。
  • 我尝试按文件夹更改/ 参数,但我仍然可以看到完整路径。

标签: nginx rewrite


【解决方案1】:

rewrite 指令有四种使用方式。 permanent 修饰符使用 http 301 响应代码执行外部重定向。通过修改浏览器中的 URL 地址栏,用户通常可以看到外部重定向。

您想要做的是一个内部重定向,它对用户不可见地执行转换。

但是,proxy_pass 指令能够执行简单的转换,而无需使用 rewrite 指令。

试试:

location / {
    proxy_pass http://192.168.0.0/folder/;
}

这将在向上游发送 URI 之前以静默方式为 /folder/ 添加前缀。

详情请参阅this documentthis document

【讨论】:

  • 我尝试按照您的建议进行操作,但列出的 404 错误目录已禁用。也许有其他解决方案而不启用它?
  • 目前我尝试了所有方法:server { listen 80; server_name subdomain.mydomain.com; location /folder/ { proxy_pass http://192.168.0.0/folder; } } 但它没有工作我仍然可以看到我的索引的完整路径
猜你喜欢
  • 1970-01-01
  • 2013-02-28
  • 1970-01-01
  • 2011-09-20
  • 1970-01-01
  • 1970-01-01
  • 2012-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多