如何为Magento的多语言SEO URL重写Nginx

时间:2012-12-15 21:04:23

标签: magento url nginx seo

我正在使用fastcgi在Nginx上开发多语言Magento。一切正常,除了我似乎无法获得像Apache一样的URL。

我想从网址中删除index.php。例如:

/es/index.php/category.html来 /es/category.html

也用英文 /index.php/category.html来 /category.html

就是这样。

其他信息:

我尝试在管理中将“使用Web服务器重写”设置为。这有效,但只适用于英语,即默认,但我获得404西班牙语,除了主页

如果我在管理中将“使用Web服务器重写”设置为,那么除了将index.php添加到URL之外,一切正常。但是,如果我从URL手动删除index.php,页面仍会转到404.

此外,在Magento管理中,我有以下基本网址设置:https://www.examplesite.com/es/和{{secure_base_url}} ../ skin / etc.

我的root / es /中有一个文件夹,其中包含index.php的副本,如下所示:

/* Store or website code */
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';

/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';

Mage::run('es');

这是我的Nigx配置:

worker_processes  1;

error_log                           /mba/nginx/logs/error.log;
pid                                 /mba/nginx/logs/nginx.pid;

events {
    worker_connections              2048;
}
http {
    include                         mime.types;
    default_type                    application/octet-stream;
    server_names_hash_bucket_size   64;
    open_file_cache                 max=100 inactive=1m; 
    open_file_cache_valid           20s;
    open_file_cache_min_uses        1;
    open_file_cache_errors          on;
    fastcgi_buffers                 256 4k; 
    ignore_invalid_headers          on; 
    client_header_buffer_size       1k;
    client_body_buffer_size         64k;
    large_client_header_buffers     4 8k;
    client_body_timeout             60;
    client_header_timeout           60;
    keepalive_requests              100;    
    keepalive_timeout               300 300;
    keepalive_disable               msie6;
    send_timeout                    60; 
    max_ranges                      1;  
    reset_timedout_connection       on; 
    sendfile        on; 
    sendfile_max_chunk              512k;
    server_tokens                   off; 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request "'
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log                      /mba/nginx/logs/access.log  main;

    autoindex off;
    map $scheme $fastcgi_https { ## Detect when HTTPS is used
        default off;
        https on;
    }

    gzip                            on;
    gzip_static                     on;
    gzip_disable                    "MSIE [1-6]\.";
    gzip_vary                       on;
    gzip_comp_level                 2;
    gzip_min_length                 0;
    gzip_proxied                    any;
    gzip_types                      text/plain image/x-icon image/bmp text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    proxy_buffering on;
    #proxy_cache_path               /mba/nginx/proxy levels=1:2 keys_zone=one:15m inactive=7d max_size=1000m;
    proxy_buffer_size               4k;
    proxy_buffers                   100 16k;
    proxy_connect_timeout           60;
    proxy_send_timeout              60;
    proxy_read_timeout              60;
    #include                         /mba/nginx/conf.d/*.conf;

    ##
    # SSL Support
    ##
    map $scheme $fastcgi_https {
    default off;
        https on;
    }

server {
    listen                          80;
    #listen                         443 default ssl;
    expires                         max;
    add_header                      Cache-Control public;   
    #ssl_certificate                /openssl/ssl/www_mybelovedangels_com.crt;
    #ssl_certificate_key            /openssl/ssl/www_mybelovedangels_com.key;
    #access_log                     /mba.nginx.1/mba/nginx/logs/access_log;
    root                            /mba.nginx.1/mba/www;

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires                         +60d;
    log_not_found                   off;
    } 
location / {
    index index.html index.php;
    try_files $uri $uri/ @handler;
    expires +30d;
    deny    192.168.1.1;
    allow   192.168.1.0/24;
    allow   127.0.0.1;
    allow   2620:100:e000::8001;
    deny    all;
    }

    # Deny access to specific directories no one
    # in particular needs access to anyways.
    location /app/ { deny all; }
    location /includes/ { deny all; }
    location /lib/ { deny all; }
    location /media/downloadable/ { deny all; }
    location /pkginfo/ { deny all; }
    location /report/config.xml { deny all; }
    location /var/ { deny all; }

    # Allow only those who have a login name and password
    # to view the export folder. Refer to /etc/nginx/htpassword.
    location /var/export/ {
        auth_basic "Restricted";
        auth_basic_user_file htpasswd;
        autoindex on;
    }

    # Deny all attempts to access hidden files
    # such as .htaccess, .htpasswd, etc...
    location ~ /\. {
         deny all;
         access_log off;
         log_not_found off;
    }

    # This redirect is added so to use Magentos
    # common front handler when handling incoming URLs.
    location @handler {
        rewrite / /index.php;
    }

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

    # Handle the exectution of .php files.
    location ~ .php$ {
        if (!-e $request_filename) {
            rewrite / /index.php last;
        }
        expires off;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param HTTPS $fastcgi_https;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param MAGE_RUN_CODE en;
        fastcgi_param MAGE_RUN_TYPE store;
        include fastcgi_params;
    }
  }
}

我希望这不是很多信息,但可能需要它。我在网上无处不寻,我找不到任何关于如何做到的事情。

由于

4 个答案:

答案 0 :(得分:1)

修改此域的 vhost 配置文件并添加以下内容:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

然后确保Magento设置为从启用使用Web服务器重写

时应该发生的URL中排除index.php

答案 1 :(得分:1)

我已经创建了一个虚拟主机并按照以下方式对我的配置进行了操作:

#user              nginx;
worker_processes  1;
error_log         /mba/nginx/logs/error.log;
pid               /mba/nginx/logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
 server_names_hash_bucket_size   64;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request "'
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /mba/nginx/logs/access.log  main;

    sendfile        on;
    autoindex off;
    map $scheme $fastcgi_https { ## Detect when HTTPS is used
        default off;
        https on;
    }

    keepalive_timeout  10;

    gzip  on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types      text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    # Load config files from the /etc/nginx/conf.d directory
    include /mba/nginx/conf.d/*.conf;

}

和我的虚拟文件一样:

server {
    listen 80;
    server_name myexamplesite.com;
    rewrite / $scheme://www.$host$request_uri permanent; ## Forcibly prepend a www
}

server {
    listen 80 default;
## SSL directives might go here
    server_name www.myexamplesite.com *.myexamplesite.com; ## myexamplesite is here twice so server_name_in_redirect will favour the www
    root /mba/www/myexamplesite.com;

    location / {
        index index.html index.php; ## Allow a static html file to be shown first
        try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
        expires 30d; ## Assume all files are cachable
    }

    ## These locations would be hidden by .htaccess normally
    location ^~ /app/                { deny all; }
    location ^~ /includes/           { deny all; }
    location ^~ /lib/                { deny all; }
    location ^~ /media/downloadable/ { deny all; }
    location ^~ /pkginfo/            { deny all; }
    location ^~ /report/config.xml   { deny all; }
    location ^~ /var/                { deny all; }

    location /var/export/ { ## Allow admins only to view export folder
        auth_basic           "Restricted"; ## Message shown in login window
        auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
        autoindex            on;
    }

    location  /. { ## Disable .htaccess and other hidden files
        return 404;
    }

    location @handler { ## Magento uses a common front handler
        rewrite / /index.php;
    }

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

    location ~ .php$ { ## Execute PHP scripts
        if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss

        expires        off; ## Do not cache dynamic content
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  HTTPS $fastcgi_https;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  MAGE_RUN_CODE en; ## Store code is defined in administration > Configuration > Manage Stores
        fastcgi_param  MAGE_RUN_TYPE store;
        include        fastcgi_params; ## See /etc/nginx/fastcgi_params
    }
}

此配置的问题是单击链接“下载”并且不更改页面。您在NginX,PHP,Magento系统或数据库日志中没有错误。

答案 2 :(得分:0)

我的设置中有点丢失,但是,您必须在nginx中动态映射您的商店代码,但现在您已经“en”硬编码了。只需包装所有配置,备份,然后重新开始。 nginx有点乱,不要继续创建硬错误的非标准配置。简单地说,就像每个magento手册一样,但是一步一步。

答案 3 :(得分:0)

ADM

我的新配置工作方式如下:

#user  www-data;
worker_processes  1; ## = CPU qty

error_log  /mba/nginx/logs/error.log;
#error_log  /mba/nginx/logs/error.log  notice;
#error_log  /mba/nginx/logs/error.log  info;

pid        /mba/nginx/logs/nginx.pid;

events {
    worker_connections  1024;
#    use epoll;
       }

http   {
    index index.html index.php; ## Allow a static html file to be shown first
    include      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"';

    #log_format error403  '$remote_addr - $remote_user [$time_local] '
    #                 '$status "$request"  "$http_x_forwarded_for"';                      

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

    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;

    keepalive_timeout   15;

    ## Use when Varnish in front
    #set_real_ip_from 127.0.0.1;
    #real_ip_header X-Forwarded-For;

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

server {   
    listen 80; ## change to 8080 with Varnish
    #listen 443 ssl; 
    server_name www.myexamplesite.com *.myexamplesite.com; ## Domain is here
    root /mba/www;

    access_log  /mba/nginx/access.log  main;

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

       ## SSL directives go here
       ##ssl_certificate = cat server.crt bundle.ca.crt >> www_server_com.chained.crt
       #ssl_certificate     /etc/ssl/certs/www_server_com.chained.crt; 
       #ssl_certificate_key /etc/ssl/certs/server.key;

       #ssl_session_cache shared:SSL:15m;
       #ssl_session_timeout 15m;

       #ssl_protocols             SSLv3 TLSv1;
       #ssl_ciphers               HIGH:!ADH:!EXPORT56;
       #ssl_prefer_server_ciphers on;

    ## 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;
        #error_page 403 /403.html;  

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

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

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

    ## These locations would be hidden by .htaccess normally
    location ~ (/(app/|includes/|/pkginfo/|var/|errors/local.xml)|/\.svn/|/.hta.+) {
        deny all;
        #internal;
        }

    ## Protecting /admin/ and /downloader/   1.2.3.4 = static ip addresses (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;}   

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
        }

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

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

    location ~ .php$ { ## Execute PHP scripts
        add_header X-Config-By 'MagenX -= www.magentomod.com =-';
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        #fastcgi_param  MAGE_RUN_CODE $storecode; ## Store code with multi domain
        fastcgi_param  MAGE_RUN_CODE en; 
        fastcgi_param  MAGE_RUN_TYPE store;
        include        fastcgi_params; ## See /etc/nginx/fastcgi_params
        }
    }
}

MAGE_RUN_CODE 中,我仍然需要添加“en”,因为它不适用于“默认”

我打算使用这个:Zoom Full-Page Cache,因为我尝试了它,它确实大大提高了我的网页速度。他说它可以配置为多语言。它在我的一些页面上弄乱了几个小地方。我确定这只是因为我没有正确地“打孔”:)。我安装了很多额外的扩展程序。

我提到它的原因是我不确定我是否应该先安装Zoom,因为在他的网站上他提供了一些代码来添加到你的NginX配置中。这就是我从中学到NginX的地方。所以我想我会松开他的Zoom并正确设置所有内容然后重新安装它。不知道真的。

无论如何,我注意到这个新的配置:

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

看起来我可能需要为每种语言设置一个新的域名?