【问题标题】:Magento, how to rewrite index.php in url ? (nginx)Magento,如何在 url 中重写 index.php? (nginx)
【发布时间】:2013-08-06 04:16:59
【问题描述】:

我有关于在 magento 中重写规则 index.php 的问题。

例如。 domain/indoor.html 是 500 内部服务器错误,但 domain/index.php/indoor.html 是可以显示的。

    My nginx.conf is 

    user  root;
    worker_processes  1;

    error_log   /var/log/nginx/error.log;

    pid        /var/run/nginx.pid;

    events {
         worker_connections  1024;
         multi_accept on;
         use epoll;
    }

http   {
index index.html index.php;
include       /etc/nginx/mime.types;
default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';                   

server_tokens       off;
sendfile            on;
tcp_nopush          on;
tcp_nodelay         on;


## Gzipping is an easy way to reduce page weight
gzip                on;
gzip_vary           on;
gzip_proxied        any;
gzip_types          text/css application/x-javascript;
gzip_buffers        16 8k;
gzip_comp_level     8;
gzip_min_length     1024;

##  SSL global settings
#ssl_session_cache shared:SSL:15m;
#ssl_session_timeout 15m;
#ssl_protocols             SSLv3 TLSv1 TLSv1.1 TLSv1.2;
#ssl_ciphers               AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH;
#ssl_prefer_server_ciphers on;

keepalive_timeout   10;

## Nginx will not add the port in the url when the request is redirected.
#port_in_redirect off; 

## Multi domain configuration
#map $http_host $storecode { 
   #www.domain1.com 1store_code; ## US main
   #www.domain2.net 2store_code; ## EU store
   #www.domain3.de 3store_code; ## German store
   #www.domain4.com 4store_code; ## different products
#}

##   Add www
server {
    listen 80;
    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}

server {   
    listen 80;
    #listen 443 ssl;
    server_name 192.168.85.114;
    root /var/www/html;
    access_log  /var/log/nginx/access_192.168.85.114.log  main;

    if ($http_user_agent = "") { return 444;}

    ####################################################################################
    ## SSL CONFIGURATION

       #ssl_certificate     /etc/ssl/certs/www_server_com.chained.crt; 
       #ssl_certificate_key /etc/ssl/certs/server.key;

    #################################################################################### 
    ## Server maintenance block. insert dev ip 1.2.3.4 static address www.whatismyip.com

    #if ($remote_addr !~ "^(1.2.3.4|1.2.3.4)$") {
        #return 503;
        #}

    #error_page 503 @maintenance;   
    #location @maintenance {
        #rewrite ^(.*)$ /error_page/503.html break;
        #internal;
        #access_log off;
        #log_not_found off;
    #}

    ####################################################################################
    ## 403 error log/page

    #error_page 403 /403.html;
    #location = /403.html {
        #root /var/www/html/error_page;
        #internal;
        #access_log   /var/log/nginx/403.log  error403;
    #}

    ####################################################################################
    ## Main Magento location

    location / {
        try_files $uri $uri/ @handler;
    }

    ####################################################################################
    ## These locations would be hidden by .htaccess normally, protected

    location ~ (/(app/|includes/|/pkginfo/|var/|errors/local.xml)|/\.svn/|/.ht.+) {
        deny all;
        #internal;
    }

    ####################################################################################
    ## Protecting /admin/ and /downloader/  1.2.3.4 = static ip (www.whatismyip.com)

    #location /downloader/  {
        #allow 1.2.3.4;  allow 1.2.3.4;  deny all;
        #rewrite ^/downloader/(.*)$ /downloader/index.php$1;
    #}
    #location /admin  {
        #allow 1.2.3.4; allow 1.2.3.4; deny all;
        #rewrite / /@handler;
    #}   

    ####################################################################################
    ## Images, scripts and styles set far future Expires header

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        open_file_cache max=10000 inactive=48h;
        open_file_cache_valid 48h;
        open_file_cache_min_uses 2;
        open_file_cache_errors off;
        expires max;
        log_not_found off;
        access_log off;
    }

    ####################################################################################
    ## Main Magento location

    location @handler {
        rewrite / /index.php?$args;
    }

    location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
        rewrite ^(.*.php)/ $1 last;
    }

    ####################################################################################  
    ## Execute PHP scripts

    location ~ .php$ {
        add_header X-UA-Compatible 'IE=Edge,chrome=1';
        try_files $uri $uri/ =404;
        #try_files $uri $uri/ @handler;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        ## Store code with multi domain
        #fastcgi_param  MAGE_RUN_CODE $storecode;
        ## Default Store code
        fastcgi_param  MAGE_RUN_CODE default; 
        fastcgi_param  MAGE_RUN_TYPE store; ## or website;
        include        fastcgi_params; ## See /etc/nginx/fastcgi_params;

        if (!-e $request_filename) { 
            rewrite / /index.php last; 
        }
    }
}

}

但这不能重写 index.php,请帮我解决这个问题。

【问题讨论】:

    标签: magento nginx php


    【解决方案1】:

    您正在创建一个循环,方法是在应该是最终位置的地方执行另一个 try_files。当我在台式机后面时,我会用一个例子来修改我的答案。

    好的,所以关于让 nginx 与 Magento 一起工作的细节。首先,您的否认有问题:

    location ~ (/(app/|includes/|/pkginfo/|var/|errors/local.xml)|/\.svn/|/.ht.+) {
    

    /pkginfo/ 不应包含前导斜杠,使用 / 最安全。而不是专门命名 .svn 和 .ht:如果你曾经切换到 Mercurial 或 Git,你也不希望它们被访问。作为旁注 - 对于 nginx .ht 不是必需的。

    您缺少 /lib/,这就是我通常将拒绝分为两部分的原因:

    location ~ ^/(app/|includes/|pkginfo/|var/|errors/local.xml|lib/|media/downloadable/) { deny all; }
    location ~ /\. { deny all; }
    

    请注意,第一个锚定在开头。这对于 /lib/ 是必要的,因为 /js/lib/ 是一个需要打开的有效路径,并且没有锚定到请求 uri 的开头,它将匹配并且您的访问者将无法加载 JavaScript 库。

    处理重写:

    首先,基地位置。设置索引,尝试 uri 并将处理程序定义为全部捕获。

    location / {
        index index.php;
        try_files $uri $uri/ @handler;
    }
    

    在处理程序中,将所有内容重写为 index.php:

    location @handler {
        rewrite /   /index.php;
    }
    

    最后,处理 php 文件。如果 try_files 没有捕获它,我们会对现有的 php 文件进行最终检查,然后拆分路径信息并传递参数。如果多重存储生效,我们还根据变量 $store 设置 MAGE_* 变量。如何映射这些,你已经在你的配置中开始了,所以我不会重复它:

    location ~ \.php(/.*)? {
        if (!-e $request_filename) {
            rewrite / /index.php last;
        }
        expires off;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^(.*\.php)(/.*)?$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param MAGE_RUN_CODE $store;
        fastcgi_param MAGE_RUN_TYPE "store";
        include fastcgi_params;
    }
    

    就是这样。希望对您有所帮助。

    【讨论】:

    • @Melvin 嘿,知道当 magento 不在根目录时如何执行此操作吗?我当前的设置是位于 domain.com/mage 中的 Magento 安装和位于 domain.com/shop1 /shop2 中的多个实际商店(修改了 index.php 以运行网站,其余的只是符号链接到 /mage)。跨度>
    【解决方案2】:

    您的问题可能出在@handler 位置,是您自己写的还是在magento 文档上写的?

    我建议尝试rewrite ^ /index.php$request_uri;

    【讨论】:

    • 你的建议还是不能解决这个问题。你确定这个问题出在@handler 吗?
    【解决方案3】:

    你应该编辑你的 httpd.conf 文件

    在 linux 中尝试添加此代码 /etc/apache2/apache2.conf 文件

    <Directory "/var/www/magento">   // Path for your site
        AllowOverride All
    </Directory> 
    

    【讨论】:

    • 对不起,我用的是 nginx 而不是 apache。
    • 这个问题与Slowpache无关。
    • 虽然答案对于 Apache 可能是正确的,但这个问题显然是关于 nginx,所以这个答案与 OP 无关。
    猜你喜欢
    • 2022-08-19
    • 2013-12-01
    • 2021-05-08
    • 2016-03-18
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    相关资源
    最近更新 更多