【问题标题】:Magento Nginx sub-page rewritesMagento Nginx 子页面重写
【发布时间】:2015-01-07 07:58:36
【问题描述】:

我正在为 Magento 从 Apache 切换到 Nginx,但似乎无法设置并正常工作。我已经在测试服务器上安装了所有东西,magento 安装正常,我可以正确访问主页和管理面板,但是如果我从前端导航到任何子页面,URL 会丢失“index.php”和给我一个 500 内部服务器错误。例如,如果我尝试 /magento/contacts/ 我会收到 500 错误,但如果我添加 /magento/index.php/contacts/ 它会加载。

我尝试了无数不同的 nginx 配置,但都没有奏效。我在这里搜索了与同一问题有关的帖子,但没有任何乐趣。我最后的希望是在这里发布我的配置文件,希望有人可以提供帮助!

我遵循了这个:https://gist.github.com/tegansnyder/96d1be1dd65852d3e576 教程,除了上面的问题,一切都很好。

任何建议将不胜感激!我在这里扯头发!提前致谢!

我有两个配置文件:

第一个配置文件

server {
server_name 192.121.166.136;
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log info;

# 504 is a PHP timeout and must be static
# 502 is momentary during a PHP restart and should be treated like maintenance
# other 50x errors are handled by Magento
error_page 502 504 /var/www/magento/504.html;

listen 80;
#listen 443 ssl;

# if you are using a load balancer uncomment these lines
# header from the hardware load balancers
#real_ip_header X-Forwarded-For;
# trust this header from anything inside the subnet
#set_real_ip_from X.X.X.1/24;
# the header is a comma-separated list; the left-most IP is the end user
#real_ip_recursive on;

# ensure zero calls are written to disk
client_max_body_size          16m;
client_body_buffer_size       2m;
client_header_buffer_size     16k;
large_client_header_buffers   8 8k;

root /var/www/;
index index.php;

fastcgi_read_timeout    90s;
fastcgi_send_timeout    60s;

# ensure zero calls are written to disk
fastcgi_buffers 512 16k;
fastcgi_buffer_size 512k;
fastcgi_busy_buffers_size 512k;

# remove the cache-busting timestamp
location ~* (.+)\.(\d+)\.(js|css|png|jpg|jpeg|gif)$ {
    try_files $uri $1.$3;
    access_log off;
    log_not_found off;
    expires 21d;
    add_header Cache-Control "public";
}

# do not log static files; regexp should capture alternate cache-busting timestamps
location ~* \.(jpg|jpeg|gif|css|png|js|ico|txt|swf|xml|svg|svgz|mp4|ogg|ogv)(\?[0-9]+)?$ {
    access_log off;
    log_not_found off;
    expires 21d;
    add_header Cache-Control "public";
}

# Server
include main.conf;
include security.conf;

}

第二个配置文件

rewrite_log on;

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

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

## force www in the URL
if ($host !~* ^www\.) {
#rewrite / $scheme://www.$host$request_uri permanent;
}

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

location /media/catalog/ {
expires 1y;
log_not_found off;
access_log off;
}

location /skin/ {
expires 1y;
}

location /js/ {
access_log off;
}

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

# for this tutorial we are going to use a unix socket
# but if HHVM was running on another host we could forego unix socket
# in favor of an IP address and port number as follows:
#fastcgi_pass 127.0.0.1:8080;

fastcgi_pass unix:/var/run/hhvm/sock;

fastcgi_index index.php;
#fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

# if you need to explictly specify a store code for Magento do it here
# this is useful if you are running multiple stores with different hostnames
#fastcgi_param MAGE_RUN_CODE default;
#fastcgi_param MAGE_RUN_TYPE store;

include fastcgi_params; ## See /etc/nginx/fastcgi_params

fastcgi_keep_conn on; #hhvm param
}

【问题讨论】:

    标签: magento nginx


    【解决方案1】:

    请完整阅读本文档:http://info.magento.com/rs/magentocommerce/images/MagentoECG-PoweringMagentowithNgnixandPHP-FPM.pdf,您可以在其中找到专门用于 Magento 的基本 ngnix 配置:

    server {
        listen 80 default;
        server_name magento.lan www.magento.lan; # like ServerName in Apache
        root /var/www/magento; # document root, path to directory with files
        index index.html index.php;
        autoindex off; # we don’t want users to see files in directories
       location ~ (^/(app/\|includes/\|lib/\|/pkginfo/\|var/\|report/config.
    xml)\|/\.svn/\|/\.git/\|/.hta.+) {
        deny all; #ensure sensitive files are not accessible
     }
    location / {
        try_files $uri $uri/ /index.php?$args; # make index.php handle requests for
    /
        access_log off; # do not log access to static files
        expires max; # cache static files aggressively
     }
    location \~\* \.(jpeg\|jpg\|gif\|png\|css\|js\|ico\|swf)$ {
        try_files $uri $uri/ @proxy; # look for static files in root directory and
       ask backend if not successful
        expires max;
        access_log off;
     }
    location @proxy {
        fastcgi_pass fpm_backend; # proxy everything from this location to backend
     }
    location \~\.php$ {
        try_files $uri =404; # if reference to php executable is invalid return 404
        expires off; # no need to cache php executable files
        fastcgi_read_timeout 600;
        fastcgi_pass fpm_backend; # proxy all requests for dynamic content to
        # backend configured in upstream.conf
        fastcgi_keep_conn on; # use persistent connects to backend
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root${fastcgi_script_name};
        fastcgi_param MAGE_RUN_CODE default; # Store code is defined in
       #administration > Configuration > Manage Stores
        fastcgi_param MAGE_RUN_TYPE store;
       } 
    }
    

    【讨论】:

    • 我已经阅读了该内容以及其他所有文章的内容,并尝试了多个不同的配置文件和位置变体,但似乎都不起作用。如果我在 magento 管理面板中关闭 Web 服务器重写,那么一切正常,但显然 index.php 被添加到每个 url。这让我发疯,我可能会回到 apache!
    • 你的配置中仍然没有任何东西代表location / { try_files $uri $uri/ /index.php?$args;,它最接近孵化RewriteRule .* index.php [L]。因为,Magento 和大多数 PHP 框架以及构建它的 ZF 一样,使用 index.php 来处理所有请求。
    【解决方案2】:

    问题是我的商店在 /var/www/magento。将文件移动到 /var/www ,一切都很好 :) 感谢各位的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多