我在Ubuntu服务器上安装了一个基本的Magento(PHP应用程序,使用index.php作为控制器)。我配置NGINX使用PHP-CGI来服务所有PHP页面。虽然网站按预期工作,但所有网址的格式均为:
http://domain.com/index.php/apparel/shoes.html
有没有办法使用nginx 重写设置,以便我可以拥有这样的网址:
http://domain.com/apparel/shoes.html
目前,这是我在配置文件中处理页面请求的内容:
# catch rewritten and standard URLs
location ~* ^(/index.php)?(.*) {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/index.php$1;
fastcgi_read_timeout 600;
}
感谢。
答案 0 :(得分:3)
我在这方面有一些nginx的经验,甚至已经写过了。因此,在一个无耻的自我推销行为中,这是我早期研究的结果:
http://www.magentocommerce.com/boards/viewreply/211050/
六年过去了,上面的链接已经死了。我的nginx配置也更成熟。以下内容从阻止所有文件开始,仅允许那些明确公开的文件。所有其他请求都会重写为index.php
。如果要执行其他PHP文件,则必须编写自己的排除项。这是我能想到的最安全的方式。
##
# Include from "server {}" block
##
# 1M = one month
expires 1M;
# recommended for all content, regardless
add_header X-Content-Type-Options "nosniff";
##
# Front handler
##
location / {
# if is evil, except when it isn't
if (-f $request_filename) {
return 403;
}
rewrite ^ /index.php last;
}
location = /index.php {
expires off;
add_header X-Content-Type-Options "nosniff";
add_header X-Xss-Protection "1; mode=block";
# "php" must be defined as an upstream
fastcgi_pass php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
uninitialized_variable_warn off;
fastcgi_param MAGE_RUN_CODE $mage_run_code if_not_empty;
fastcgi_param MAGE_RUN_TYPE $mage_run_type if_not_empty;
fastcgi_param MAGE_IS_DEVELOPER_MODE $mage_is_developer_mode if_not_empty;
fastcgi_param APPLICATION_ENV $application_env if_not_empty;
# same as max_execution_time in .htaccess
fastcgi_read_timeout 18000s;
}
##
# Whitelist public static files
#
# avoid regex where possible
##
location ^~ /media/ {
# sometimes files are retrieved from database with get.php
try_files $uri @mediaget;
}
# there is no way to call get.php directly
location @mediaget {
add_header X-Content-Type-Options "nosniff";
add_header X-Xss-Protection "1; mode=block";
fastcgi_pass php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/get.php;
}
location ^~ /skin/ {
# allowed with defaults
}
location ^~ /js/ {
# allowed with defaults
}
# should not be accessed normally since real favicon is in /skin/frontend
# however this is conventional
location = /favicon.ico {
# pretty sure that it exists but just in case...
log_not_found off;
}
# specific exclusions, still no regex
location = /media/.htaccess { return 404; }
location ^~ /media/customer/ { return 404; }
location ^~ /media/downloadable/ { return 404; }
##
# Error handling
##
# possibly change this to Magento's no-route path
error_page 404 /errors/404.php;
location = /errors/404.php {
fastcgi_pass php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/errors/404.php;
}
# used when site is in maintenance mode
error_page 500 503 /errors/503.php;
location = /errors/503.php {
fastcgi_pass php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/errors/503.php;
}
# empty blocks allow serving without modification
# ideals for gzip and mime-type are already loaded
location ^~ /errors/default/css/ { }
location ^~ /errors/default/images/ { }
location ^~ /errors/enterprise/css/ { }
location ^~ /errors/enterprise/images/ { }
upstream php {
server 127.0.0.1:9000;
# add more PHP-FPM processes here if you have them, or maybe a unix socket
}
然后每个商店都有自己的服务器块,类似于:
server {
server_name yoursite.com;
root /var/www;
include magento_server;
set $mage_run_code "default";
set $mage_run_type "website";
}
由于.htaccess
文件被忽略(因为这不是Apache),因此在每个Web根目录中放置.user.ini
会很有帮助:
max_execution_time=600
memory_limit=256M
session.auto_start=off
session.gc_maxlifetime=31536000
答案 1 :(得分:0)
您可以尝试添加类似于此
的重写规则rewrite ^/(.*)/?$ /index.php/$1 break;
抱歉,这是未经测试的,但它应该是关闭的。更多信息: http://wiki.nginx.org/NginxHttpRewriteModule
答案 2 :(得分:0)
我无法在上面的答案中找到" /index.php/foo/bar"被永久地重定向到" / foo / bar"。经过一段时间的努力消除无限重定向问题后,我已经确定了以下初始配置来实现这一目标:
server {
listen 80;
root /path/to/doc/root/;
server_name foobar.com;
# Perform 301 canonical redirect to remove index.php
location ^~ /index.php {
rewrite ^/index.php(.*)$ $scheme://$server_name$1 permanent;
}
#catch-all location block for all other URLs
location / {
#note: do not add "$uri/" before @handler, if you have an "index index.php" directive, it will auto redirect to "index.php" and cause an infinite loop due to the location block above
try_files $uri @handler;
}
#internally add index.php back to the request
location @handler {
rewrite ^ /index.php$uri break;
include php.conf;
}
#this will also allow other php files to be handled correctly
#be careful here, see http://wiki.nginx.org/Pitfalls#Passing_Uncontrolled_Requests_to_PHP
location ~ \.php {
include php.conf;
}
}
其中php.conf包含标准的FastCGI参数设置,例如:
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;