如何从地址中删除index.php,同时通过fastcgi将请求重定向到index.php?

发布于 2024-09-27 20:51:27 字数 751 浏览 4 评论 0原文

我在 Ubuntu 服务器上有一个基本的 Magento(PHP 应用程序,使用 index.php 作为控制器)设置。我将 NGINX 配置为使用 PHP-CGI 来服务所有 PHP 页面。当网站按预期工作时,所有 URL 的形式如下:
http://domain.com/index.php/apparel/shoes.html

有没有办法使用 nginx rewrite 设置,以便我可以使用这样的 URL:
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;
}

谢谢。

I have a basic Magento (PHP app, using index.php as the controller) setup on an Ubuntu server. I configured NGINX to use PHP-CGI to serve all PHP pages. While the site is working as expected, all the URLs are of the form:
http://domain.com/index.php/apparel/shoes.html

Is there a way to use the nginx rewrite setting so that I can have the URLs like this instead:
http://domain.com/apparel/shoes.html

Currently, this is what I have in the configuration file to handle page requests:

# 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;
}

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终陌 2024-10-04 20:51:27

我在这方面对 nginx 有一些经验,甚至还写过相关文章。因此,在无耻的自我推销行为中,这是我早期研究的结果:
http://www.magentocommerce.com/boards/viewreply/211050/


六年过去了,上面的链接已经失效了。而且我的 nginx 配置更加成熟。下面首先阻止所有文件并只允许那些明确公开的文件。所有其他请求都将重写到 index.php。如果您想执行其他 PHP 文件,您必须编写自己的排除。这是我能想到的最安全的方法。

/etc/nginx/magento_server:/etc/nginx/conf.d/php.conf

##
# 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
}

每个商店都有自己的服务器块,类似于:

/etc/nginx/sites-enabled/yoursite.com:

server {
    server_name        yoursite.com;
    root               /var/www;
    include            magento_server;
    set $mage_run_code "default";
    set $mage_run_type "website";
}

自从 .htaccess 文件被忽略(因为这不是 Apache),它有助于将 .user.ini 放入每个 Web 根目录:

/var/www/.user.ini

max_execution_time=600
memory_limit=256M
session.auto_start=off
session.gc_maxlifetime=31536000

I have some experience with nginx in this regard and have even written about it. So in a shameless act of self-promotion here is the result of my earlier research:
http://www.magentocommerce.com/boards/viewreply/211050/


Six years on and the above link is dead. Also my nginx configs are more mature. The following starts by blocking all files and only allowing those which are explicitly public. All other requests are rewritten to index.php. If you want to execute some other PHP file you must write your own exclusion. This is the safest way I can think of.

/etc/nginx/magento_server:

##
# 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/   { }

/etc/nginx/conf.d/php.conf:

upstream php {
    server 127.0.0.1:9000;
    # add more PHP-FPM processes here if you have them, or maybe a unix socket
}

Each store then has it's own server block similar to this:

/etc/nginx/sites-enabled/yoursite.com:

server {
    server_name        yoursite.com;
    root               /var/www;
    include            magento_server;
    set $mage_run_code "default";
    set $mage_run_type "website";
}

And since .htaccess files are ignored (because this is not Apache) it helps to put .user.ini in each web root:

/var/www/.user.ini

max_execution_time=600
memory_limit=256M
session.auto_start=off
session.gc_maxlifetime=31536000
落叶缤纷 2024-10-04 20:51:27

您可以尝试添加类似于此的重写规则

rewrite ^/(.*)/?$ /index.php/$1 break;

抱歉,它未经测试,但应该很接近。更多信息:
http://wiki.nginx.org/NginxHttpRewriteModule

You could try adding a rewrite rule similar to this

rewrite ^/(.*)/?$ /index.php/$1 break;

Sorry, it's untested, but it should be close. More info:
http://wiki.nginx.org/NginxHttpRewriteModule

彩扇题诗 2024-10-04 20:51:27

我在上面的答案中看不到“/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;

I can't see anywhere in the answers above where "/index.php/foo/bar" was being redirected permanently to "/foo/bar". After a bit of mucking around to eliminate the infinite redirect problem, I've settled on the following initial config to achieve this:

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;
  }
}

where php.conf contains the standard FastCGI parameter setup, for example:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文