【问题标题】:Php dynamic subpage and nginx address rewritingphp动态子页面和nginx地址改写
【发布时间】:2017-03-09 19:17:44
【问题描述】:

我创建了一个基于 ajax 技术的网站。现在我想使用php(nginx)实现类似的效果。问题是我不想为每个用户或报价或任何东西创建(甚至动态).php 文档。我有每个页面的模板,例如userTemplate.php,它适用于$_GET 方法,它用正确的数据填充它但地址是不可接受的,我可以将请求example.com/userTemplate.php?id=1 转换为example.com/users/1. 我尝试重写它在配置文件中,但随后它将我重定向到路径/users/1,这是错误的......可能吗?请我已经花了几个小时寻找任何答案。

【问题讨论】:

  • 恕我直言,如果您将现有的 nginx 配置添加到您的问题中,您将更有可能得到答案。
  • 好的,但现在它是纯粹的:code location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; # # With php5-cgi alone: # fastcgi_pass 127.0.0.1:9000; # # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; } 应该有什么让它工作?

标签: javascript php jquery .htaccess nginx


【解决方案1】:

试试这个:

location / {
    try_files $uri $uri/ =404;
}
location ~* /users/(\d+) {
    try_files $uri /userTemplate.php?id=$1;
}
location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    #
    # With php5-cgi alone:
    # fastcgi_pass 127.0.0.1:9000;
    #
    # With php5-fpm: 
    fastcgi_pass unix:/var/run/php5-fpm.sock;
}

如果您想采用另一种方法,请查看Creating NGINX Rewrite Rules。如果你需要不同的、更复杂的正则表达式,或许可以从here开始。

【讨论】:

  • 非常感谢!它解决了我的问题,终于奏效了!我只需要在 try_files 之后添加“$uri”:)
  • 对,添加了$uri
猜你喜欢
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
  • 2011-01-27
  • 2012-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多