Redirect 子文件夹的Nginx重写规则
我正在尝试重写一个url,如下所示:Redirect 子文件夹的Nginx重写规则,redirect,nginx,url-rewriting,http-redirect,Redirect,Nginx,Url Rewriting,Http Redirect,我正在尝试重写一个url,如下所示: https://example.com/products/product-post ---> https://example.com/product-post 但我同时想要urlhttps://example.com/products/无需任何修改即可访问,因为它是一个产品目录 这不起作用,导致500服务器错误: location /products/ { rewrite ^/(.*)$ /products/$1 last; } 出于组织原
https://example.com/products/product-post ---> https://example.com/product-post
但我同时想要urlhttps://example.com/products/
无需任何修改即可访问,因为它是一个产品目录
这不起作用,导致500服务器错误:
location /products/ {
rewrite ^/(.*)$ /products/$1 last;
}
出于组织原因,我将文件保存在/products/file1、file2等文件中。
也许我应该用“别名”而不是“重写”
多谢各位
更新:下面是我的server.conf配置
server {
server_name www.example.com;
listen 80;
listen 443 ssl spdy;
listen [::]:80;
listen [::]:443 ssl spdy;
ssl_certificate /opt/ssl/example.com.chained.crt;
ssl_certificate_key /opt/ssl/example.com.key;
# Non-www redirect
return 301 https://example.com$request_uri;
}
server {
server_name example.com;
listen 443 ssl spdy;
listen [::]:443 ssl spdy;
root /home/html_public;
charset UTF-8;
ssl_certificate /opt/ssl/example.com.chained.crt;
ssl_certificate_key /opt/ssl/example.com.key;
add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
keepalive_timeout 70;
ssl_buffer_size 1400;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=86400;
resolver_timeout 10;
ssl_trusted_certificate /opt/ssl/example.com.chained.crt;
location ~* \.(jpg|jpeg|gif|png|ico|cur|gz|svgz|mp4|ogg|ogv|webm|htc|css|js|otf|eot|svg|ttf|woff|woff2)(\?ver=[0-9.]+)?$ {
expires 1M;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
access_log off;
}
#access_log logs/host.access.log main;
#===BAN COUNTRIES START ==============
if ($allowed_country = no) {
return 403;
}
if ($bad_referer) {
return 444;
}
location @extensionless-php {
rewrite ^(.*)/$ $1.php last;
rewrite ^(.*[^/])$ $1/ permanent;
}
location / {
try_files $uri $uri/ @extensionless-php;
#limit_conn num_conn 15;
#limit_req zone=num_reqs;
}
#=====PAGE SPEED START==========
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }
#=====PAGE SPEED END==========
error_page 404 /404.php;
#pass the PHP scripts to FastCGI server listening on php-fpm unix socket
location ~ \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
include fastcgi_params;
}
location = /robots.txt {
access_log off;
log_not_found off;
}
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
#=====START phpMyAdmin==============#
location /phpMyAdmin {
root /usr/share/;
location ~ ^/phpMyAdmin/(.+\.php)$ {
root /usr/share/;
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* ^/phpMyAdmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpmyadmin {
rewrite ^/* /phpMyAdmin last;
}
#=====END phpMyAdmin==============#
location /webmail {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/webmail/(.+\.php)$ {
root /usr/share/;
try_files $uri =404;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* ^/webmail/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
#====Out Link REDIRECTS===============
location /go/ {
rewrite ^/go/(.*)$ /go/site-linker.php?site=$1 last;
}
location ~ /products/(.+) {
rewrite ^/products/(.*)$ /$1 last;
}
}
在日志中,我得到了以下信息:
2016/04/24 12:07:45 [notice] 6810#0: *1 "^/products/(.*)$" does not match "/xperia-z5/", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"
2016/04/24 12:07:45 [notice] 6810#0: *1 "^/products/(.*)$" does not match "/xperia-z5/", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"
2016/04/24 12:07:45 [notice] 6810#0: *1 "^(.*)/$" matches "/xperia-z5/", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"
2016/04/24 12:07:45 [notice] 6810#0: *1 "^(.*)/$" matches "/xperia-z5/", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"
2016/04/24 12:07:45 [notice] 6810#0: *1 rewritten data: "/xperia-z5.php", args: "", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"
2016/04/24 12:07:45 [notice] 6810#0: *1 rewritten data: "/xperia-z5.php", args: "", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"
我已经更新了我的重写示例配置-
从那个教程-如果您使用重写
,索引
或类似的东西,nginx将在内部重新处理请求。为了解决这个问题,我添加了prods
目录-因此对于…/products/nginx services/prods/index.html-而对于所有其他…/products/(.+)-请求被重定向到root。所以,如果您键入-它将从根目录显示index.html,而不更改浏览器中的路径。希望能有帮助……:)
我已经更新了我的重写示例配置-
从那个教程-如果您使用
重写
,索引
或类似的东西,nginx将在内部重新处理请求。为了解决这个问题,我添加了prods
目录-因此对于…/products/nginx services/prods/index.html-而对于所有其他…/products/(.+)-请求被重定向到root。所以,如果您键入-它将从根目录显示index.html,而不更改浏览器中的路径。希望能有帮助……:) 经过大量的尝试和错误,结果是:
location / {
try_files $uri $uri/ @extensionless-php;
rewrite /(.+$) /products/$1 break;
}
location = /products/ {
index index.php;
}
我希望这将对将来的人有所帮助。经过大量的尝试和错误,解决方案是:
location / {
try_files $uri $uri/ @extensionless-php;
rewrite /(.+$) /products/$1 break;
}
location = /products/ {
index index.php;
}
我希望这会对将来的人有所帮助。你能重写整个
产品/产品帖子吗?这样就可以了…@MichałZaborowski如果我这样做,我会得到一个404:location/products/(.*)$/{rewrite^/(.*)$/products/$1 last;}
-请验证这是否会破坏您的系统,因为需要sodo。更新配置文件中的“重写”,然后您可以看到发生了什么-请参阅日志file@RichardSmith是的,产品帖子只是一个页面。什么都可以。想象一下:domain/category/post/-->domain/post/
@RichardSmith我的意思是我想把domain/category/post/
重写成domain/post/
。场景是,用户要查看域/post/
,但要获取域/category/post/
的服务器可以重写整个产品/product post
?这样就可以了…@MichałZaborowski如果我这样做,我会得到一个404:location/products/(.*)$/{rewrite^/(.*)$/products/$1 last;}
-请验证这是否会破坏您的系统,因为需要sodo。更新配置文件中的“重写”,然后您可以看到发生了什么-请参阅日志file@RichardSmith是的,产品帖子只是一个页面。什么都可以。想象一下:domain/category/post/-->domain/post/
@RichardSmith我的意思是我想把domain/category/post/
重写成domain/post/
。这种情况是,用户需要查看domain/post/
,但服务器需要再次获取domain/category/post/
。运气不好。我认为location@extensionless php
可能有问题,看看更新的问题,我已经包含了我的配置文件。打开日志记录:error\u log/var/log/nginx/error.log debug;重写你的登录代码>。看看哪里出了问题。其他选项-请提供4-5个示例URL和要执行的目标脚本。我将编写测试来跟踪问题的进展,因为日志太大,无法添加为注释。显然出了问题。在第二个服务器级别添加:error\u log/var/log/nginx/error.log debug;重写你的登录
-thentail-f/var/log/nginx/error.log
在服务器上,然后触发页面。在控制台中,您将看到正在发生的事情-所有内部重写、正则表达式处理、内部重定向后的重新处理、头。。。你说的我已经做过了。ngx_http_rewrite_模块指令处理,结果进入通知级别的错误日志。这就是为什么你会在日志中再次看到[注意]
(我更新了问题)。运气不好。我认为location@extensionless php
可能有问题,看看更新的问题,我已经包含了我的配置文件。打开日志记录:error\u log/var/log/nginx/error.log debug;重写你的登录代码>。看看哪里出了问题。其他选项-请提供4-5个示例URL和要执行的目标脚本。我将编写测试来跟踪问题的进展,因为日志太大,无法添加为注释。显然出了问题。在第二个服务器级别添加:error\u log/var/log/nginx/error.log debug;重写你的登录
-thentail-f/var/log/nginx/error.log
在服务器上,然后触发页面。在控制台中,您将看到正在发生的事情-所有内部重写、正则表达式处理、内部重定向后的重新处理、头。。。你说的我已经做过了。ngx_http_rewrite_模块指令处理,结果进入通知级别的错误日志。这就是为什么您会在日志中看到[注意]
(我更新了问题)