Magento – 500 Internal Server Error nginx/1.12.0 on checkout page after Migration from Apache to Nginx

apachemagento-1.9nginx

i'm facing a problem with nginx rewrites after i migrate my Magento website from Apache to Nginx!
When i add a product to the cart and i press the button Place the order is giving me 500 internal server error !
I opened my logs and i can see this error message :

2017/05/26 13:03:39 [error] 48314#48314: *4022 could not find named location "@handler", client: 81.149.36.86, server: abcsports.co.uk, request: "GET /index.php/checkout/onepage/ HTTP/1.1", host: "www.....", referrer: "http://www........"

My conf file is looking like that :

Redirect to www.

server { listen 80; server_name abcsports.co.uk; return 301 $scheme://www.$host$request_uri; }

Set FPM pool socket for Magento Dashboard, based on adminhtml cookie

map $http_cookie $phpfpm_socket { default unix:/var/run/php-fpm/abcsports.co.uk.sock; ~adminhtml unix:/var/run/php-fpm/abcsports.co.uk-admin.sock; }

server { listen 80 default_server; server_name www.abcsports.co.uk media.abcsports.co.uk skin.abcsports.co.uk js.abcsports.co.uk; # root /var/www/vhosts/abcsports.co.uk/httpdocs; root /var/www/html/abcsports.co.uk/public_html/;

access_log /var/log/nginx/abcsports.co.uk-access.log; error_log /var/log/nginx/abcsports.co.uk-error.log;

client_body_buffer_size 8k; client_max_body_size 10M; client_header_buffer_size 1k; large_client_header_buffers 4 16k;

# SSL Termination if ($server_port = 80) { set $httpss off; } if ($http_x_forwarded_proto = https) { set $httpss on; }

location / { index index.html index.php; try_files $uri $uri/ @handler; expires 30d; }

location ~ ^/(app|includes|media/downloadable|pkginfo|report/config.xml|var)/ { deny all; }

location /. { return 404; }

location @handler { rewrite / /index.php; } #location ~ .php/ { rewrite ^(.*.php)/ last; }

location ~ .php$ { #if (!-e $request_filename) { rewrite / /index.php last; } expires off; fastcgi_pass $phpfpm_socket; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param MAGE_RUN_CODE default; fastcgi_param MAGE_RUN_TYPE store; fastcgi_param HTTPS $httpss; include fastcgi_params; fastcgi_buffer_size 32k; fastcgi_buffers 512 32k; fastcgi_read_timeout 300; } }

server { listen 443 ssl; server_name abcsports.co.uk www.abcsports.co.uk media.abcsports.co.uk skin.abcsports.co.uk js.abcsports.co.uk; root /var/www/html/abcsports.co.uk/public_html/; ssl on; ssl_certificate /etc/ssl/certs/abcsports.co.uk.pem; ssl_certificate_key /etc/ssl/certs/abcsports.co.uk.key;

access_log /var/log/nginx/abcsports.co.uk-ssl-access.log;
error_log /var/log/nginx/abcsports.co.uk-ssl-error.log;

location / {
index index.html index.php;
try_files $uri $uri/ @handler;
expires 30d;
}

}

I looked everywhere on the internet and i read a lot of things but didn't find the solution ! I would appreciate any help !
Thank you !

Best Answer

(1) This configuration file is derived from your file and is created with the assumption that you want to force SSL on your primary domain, abcsports.co.uk. This is recommended.

(2) I moved the subdomains, "media.abcsports.co.uk, skin.abcsports.co.uk, and js.abcsports.co.uk" into separate server blocks. By default, SSL is not forced for these subdomains, but you could enable the redirections as instructed in the configuration file. Ensure that the SSL certificates for abcsports.co.uk is applicable to the subdomains before activating the SSL block for the subdomains.

(3) There could be errors. Use the error log to debug them.

(4) Backup the current vhost file because using this one. You can do this during a low traffic period.

server {
    listen 80;
    server_name         media.abcsports.co.uk skin.abcsports.co.uk js.abcsports.co.uk;
    # root              /var/www/vhosts/abcsports.co.uk/httpdocs;
    root                /var/www/html/abcsports.co.uk/public_html;

    location / {
        index           index.php;
        try_files       $uri $uri/ /index.php?$args;
        expires         30d;
    }

    access_log          /var/log/nginx/abcsports.co.uk-access.log;
    error_log           /var/log/nginx/abcsports.co.uk-error.log;
}

# Enable this server block if you want to enable ssl redirection to the SSL block below. Ensure you disable the above HTTP block, you enable this block.
#server {
#    listen             80;
#    server_name        media.abcsports.co.uk skin.abcsports.co.uk js.abcsports.co.uk;
#   server_tokens       off;
#    return             301 https://$server_name$request_uri;
#}

#server {
    #listen                 443 ssl;
    #server_name            media.abcsports.co.uk skin.abcsports.co.uk js.abcsports.co.uk;
    #root                   /var/www/html/abcsports.co.uk/public_html/;
    #ssl_certificate        /etc/ssl/certs/abcsports.co.uk.pem; # Ensure certificate is wildcard and applies to subdomains of abcsports.co.uk
    #ssl_certificate_key    /etc/ssl/certs/abcsports.co.uk.key; # Ensure certificate is wildcard and applies to subdomains of abcsports.co.uk


    #access_log                 /var/log/nginx/abcsports.co.uk-ssl-access.log;
    #error_log              /var/log/nginx/abcsports.co.uk-ssl-error.log;

    #location / {
        #index              index.html index.php;
        #try_files          $uri $uri/ @handler;
        expires             30d;
    #}

#}

server {
    listen              80 default_server;
    server_name         abcsports.co.uk www.abcsports.co.uk;
    server_tokens       off;
    return              301 https://www.abcsports.co.uk$request_uri;
}

server {
    listen              443 ssl;
    #listen             443 http2 ssl; uncomment this line and comment line 9 if Nginx was compiled with http2
    server_name         abcsports.co.uk;
    server_tokens       off;
    return 301          https://www.abcsports.co.uk$request_uri;
}

server {
    listen              443 ssl default_server;
    #listen             443 http2 ssl default_server;
    server_name         www.abcsports.co.uk;
    root                /var/www/html/abcsports.co.uk/public_html;
    index               index.php;
    access_log          /var/log/nginx/abcsports.co.uk-ssl-access.log combined;
    error_log           /var/log/nginx/abcsports.co.uk-ssl-error.log error;

    ssl_certificate      /etc/ssl/certs/abcsports.co.uk.pem;
    ssl_certificate_key  /etc/ssl/certs/abcsports.co.uk.key;

    client_body_buffer_size         128K;
    client_body_timeout             3m;
    client_header_buffer_size       3m;
    large_client_header_buffers     4 256k;
    client_header_timeout           3m;
    client_max_body_size            100M;

    gzip                on;
    gzip_proxied        any;
    gzip_types          text/plain text/css application/json application/x-javascript text/xml 
    application/xml     application/xml+rss text/javascript application/javascript text/x-js; 
    gzip_vary           on;
    gzip_comp_level     6;
    gzip_buffers        16 8k; 
    gzip_disable        "MSIE [1-6]\.(?!.*SV1)";

    #add_header         Strict-Transport-Security "max-age=31536000"; #Enable this after you understand the implications of STS

    location / {
        try_files       $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
        expires         30d; ## Assume all files are cachable

        location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
            expires     max;
        }
    }

    map $http_cookie $phpfpm_socket {
        default unix:/var/run/php-fpm/abcsports.co.uk.sock;
        ~adminhtml unix:/var/run/php-fpm/abcsports.co.uk-admin.sock;
    }

    location ~ \.php$ {
        charset        utf-8;
        default_type   text/html;

        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        fastcgi_param           SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass            $phpfpm_socket;
        fastcgi_param           MAGE_RUN_CODE default;
        fastcgi_param           MAGE_RUN_TYPE store;
        #fastcgi_param          HTTPS $httpss; #No longer needed
        fastcgi_read_timeout    300;
        fastcgi_keep_conn       on;
        fastcgi_buffer_size     32k;
        fastcgi_buffers 512     32k;
        fastcgi_read_timeout    300;
        fastcgi_index           index.php;
        #include                /etc/nginx/fastcgi_params; #To include additional fastcgi_param
    }

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

    # Enable to restrict access to your downloader folder 
    #location /downloader {
        #error_page 403 = @deny_downloader;
        #allow  xx.xx.xx.xx; #Change to your static or current dynamic IP. Restart Nginx after each modification. You can duplicate this line.
        #deny all;
        #index  index.php;
        #try_files $uri $uri/ /index.php?$args;
    #}

    location @deny_downloader {
        return 303 https://www.abcsports.co.uk;
    }

    location ~ .php/ {
        rewrite ^(.*.php)/ $1 last;
    }

    location ~ /.well-known {
        allow all;
    }

    ## These locations would be hidden by .htaccess normally
    location ^~ /app/                { deny all; }
    location ^~ /includes/           { deny all; }
    location ^~ /lib/                { deny all; }
    location ^~ /var/                { deny all; }
    location /var/export/            { deny all; }
    location /media/customer/        { deny all; }
    location /media/downloadable/    { deny all; }
    location ~ cron\.php             { deny all; }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one    
    location ~ /\.ht {
        deny  all;
    }

    location /. {
        return 404;
    }

    # Enable for custom error files
    #error_page  403 /error/404.html;
    #error_page  404 /error/404.html;
    #error_page  500 502 503 504 /error/50x.html;

    #location /error/ {
        #alias   /home/admin/web/abcsports.co.uk/document_errors/;
    #}

    location ~* "/\.(htaccess|htpasswd)$" {
        deny    all;
        return  404;
    }
}

Expecting your feedback. Cheers!