php - Nginx 重写规则未按预期工作
问题描述
我有一个网站
https://dev.mywebsite.com/index.php?album=portraits
,它根据 POST 值动态显示相册。
我想将此 URL 重写为:
https://dev.mywebsite.com/portraits
但是我的 Nginx 重写规则不起作用。当我进入时什么都没有发生https://dev.mywebsite.com/index.php?album=portraits
。并且进入时没有找到页面https://dev.mywebsite.com/portraits
。
我不知道我做错了什么。
这是我目前正在尝试使用的代码:
location / {
rewrite ^/(.*)$ /album.php?album=$1 last;
}
我也试过这个:
location = /portraits {
rewrite ^/portraits?$ /index.php?album=portraits break;
}
和这个:
location = /album {
rewrite ^album/([a-z]+)/?$ album.php?album=$1 break;
}
这是我正在使用的整个 nginx 站点配置文件:
server {
listen 80 default_server;
listen 443 ssl;
root /config/www;
index index.php index.htm index.html;
server_name dev.mywebsite.com;
ssl_certificate /config/keys/cert.crt;
ssl_certificate_key /config/keys/cert.key;
client_max_body_size 0;
location / {
try_files $uri $uri/ /index.html /index.php?$args =404;
}
location / {
rewrite ^/(.*)$ /album.php?album=$1 last;
}
location ~ \.php$ {
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
解决方案
如果你真正拥有的文件是index.php
,那么你可以重写(没有任何额外location
的):
rewrite ^/portraits$ /index.php?album=portraits last;
保持location ~ \.php$ {
当然:)
推荐阅读
- shell - Shell 脚本 - 知道包含的路径,然后包含另一个文件
- react-native - 如何知道 FlatList 中滚动(向上或向下)的状态?
- android - 我们如何在没有互联网连接(离线)的情况下使用 android developer basics V2(概念和代码实验室)?
- node.js - MongoSkin“无法读取未定义的属性‘应用’”
- bash - “别名:=:未找到”,并且未定义别名,在 .bashrc 中使用“别名 ll = 'ls -l'”
- node.js - 将电子应用程序最小化到系统托盘后,窗口调整大小不起作用
- python - 强制所有列都是 pandas 中的字符串
- javascript - jQuery 序列化数据未成功发布到 PHP 表单处理程序
- git - 在 git 存储库中,是否可以在克隆的配置中预先指定“远程”?
- python - Python:无法使用 BeautifulSoup