pth*_*fan 53 nginx url-rewriting
在我的apache配置中,我有以下简单的重写规则
我怎样才能在nginx中重写这个?
#
# Redirect all to index.php
#
RewriteEngine On
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (/[^.]*|\.)$ [NC]
RewriteRule .* index.php [L]
Run Code Online (Sandbox Code Playgroud)
这是我的nginx服务器块现在的样子,但它不起作用:(
root /home/user/www;
index index.php;
# Make site accessible from http://localhost/
server_name some-domain.dev;
###############################################################
# exclude /favicon.ico from logs
location = /favicon.ico {
log_not_found off;
access_log off;
}
##############################################################
# Disable logging for robots.txt
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
##############################################################
# Deny all attempts to access hidden files such as
# .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
##############################################################
#
location / {
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php$args;
fastcgi_pass 127.0.0.1:9000;
}
###############################################################
# serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
expires 30d;
}
###############################################################
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
Run Code Online (Sandbox Code Playgroud)
小智 111
完美的解决方案我已经尝试过并在我的网站配置文件中附加此代码后成功获取索引页面.
location / {
try_files $uri $uri/ /index.php;
}
Run Code Online (Sandbox Code Playgroud)
在配置文件本身解释说,"首先尝试将请求作为文件,然后作为目录,然后回到index.html,在我的情况下,它是index.php,因为我通过PHP代码提供页面.
Jan*_* Wy 58
要传递get变量,请使用$args:
location / {
try_files $uri $uri/ /index.php?$args;
}
Run Code Online (Sandbox Code Playgroud)
jag*_*ler 32
1除非文件存在,否则将重写为index.php
将以下内容添加到您的 location ~ \.php$
try_files = $uri @missing;
Run Code Online (Sandbox Code Playgroud)
这将首先尝试提供文件,如果没有找到它将移动到该@missing部分.所以还要将以下内容添加到您的配置(location块外),这将重定向到您的索引页面
location @missing {
rewrite ^ $scheme://$host/index.php permanent;
}
Run Code Online (Sandbox Code Playgroud)
2在网址上你永远不会看到文件扩展名(.php)
删除php扩展读取以下内容:http: //www.nullis.net/weblog/2011/05/nginx-rewrite-remove-file-extension/
和链接中的示例配置:
location / {
set $page_to_view "/index.php";
try_files $uri $uri/ @rewrites;
root /var/www/site;
index index.php index.html index.htm;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/site$page_to_view;
}
# rewrites
location @rewrites {
if ($uri ~* ^/([a-z]+)$) {
set $page_to_view "/$1.php";
rewrite ^/([a-z]+)$ /$1.php last;
}
}
Run Code Online (Sandbox Code Playgroud)
iut*_*nvg 15
无需重写的扁平和简单配置,在某些情况下可以工作:
location / {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /home/webuser/site/index.php;
}
Run Code Online (Sandbox Code Playgroud)
使用nginx $ is_args代替?对于GET查询字符串
location / { try_files $uri $uri/ /index.php$is_args$args; }
Run Code Online (Sandbox Code Playgroud)
这是你的第二个问题的答案:
location / {
rewrite ^/(.*)$ /$1.php last;
}
Run Code Online (Sandbox Code Playgroud)
这对我有用(根据我的经验),意味着你所有的 blabla.php 将重写为 blabla
就像http://yourwebsite.com/index.php到http://yourwebsite.com/index
| 归档时间: |
|
| 查看次数: |
139058 次 |
| 最近记录: |