nginx - Nginx 重写无法正常工作
问题描述
我想将重写 /author/xxx 应用到 /index.php?author_name=xxx
rewrite ^/author/(.*)/$ /index.php?author_name=$1;
当我应用代码时,浏览器 url 看起来像这样:
http://www.site-name.com/?author_name=xxx
我希望 url 看起来像:
http://www.site-name.com/author/xxx但它会重写 index.php?author_name=xxx
有什么想法可以解决吗?
我的 Nginx conf 文件如下:
server {
server_name site-name.com;
rewrite ^(.*) http://www.site-name.com$1 permanent;
listen 8080;
}
server {
listen 8080;
access_log off;
# access_log /home/www.site-name.com/logs/access_log;
error_log on;
# error_log /home/www.site-name.com/logs/error.log;
#add_header X-Frame-Options SAMEORIGIN;
#add_header X-Frame-Options "ALLOW-FROM https://www.site-name.xyz";
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
root /home/www.site-name.com/public_html;
include /etc/nginx/conf/ddos2.conf;
# include /etc/nginx/conf/cors.conf;
index index.php index.html index.htm;
server_name www.site-name.com;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
rewrite ^/author/(.*)/$ /index.php?author_name=$1;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_connect_timeout 250;
fastcgi_send_timeout 250;
fastcgi_read_timeout 250;
fastcgi_buffer_size 256k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME /home/www.site-name.com/public_html$fastcgi_script_name;
}
include /etc/nginx/conf/drop.conf;
}
解决方案
你rewrite
看起来不错:从外部你可以使用/author/...
,在内部它将使用index.php?...
.
我想您还想重定向index.php?...
到不错的路径:
rewrite ^/author/([^/]*)/?$ /index.php?author_name=$1 break;
rewrite ^/index.php$ https://www.example.com/author/$arg_author_name/ redirect;
root /var/www/html/;
fastcgi_index index.php;
fastcgi_pass php;
include fastcgi_params;
推荐阅读
- oracle - 我可以更改“ORA_MAX_NAME_LEN”的值吗?
- php - 使用第三方库记录来自 PHP 应用程序的所有 cURL 请求
- jquery - 如何在 jQuery 中分别显示接收消息?
- python - 在python中迭代两个不同大小的列表
- odoo - 如何根据 Odoo 11 上的购买价格更改产品价格?
- node.js - MongoDb 查询:DB 中的 200 万个查询或 10000 个块中的 200 个查询。哪一个对性能更好?
- jpa - JPA Criteria API - 填充关联
- php - PHP preg_replace 删除方括号之间的所有点字符
- c++ - 使用基于范围的 for 循环填充指针向量
- java - 在 xml 中使 ConcurrentMapCacheManager 事务感知