I am in the process of switching from Apache to Nginx for Magento but cannot seem to get it set up and working correctly. I have installed everything on a test server and the magento install went fine and I can access both the home page and the admin panel correctly but if I navigate to any sub-page from the frontend, the URL loses the "index.php" and gives me a 500 internal server error. For example if I try /magento/contacts/ I get a 500 error but if I add /magento/index.php/contacts/ it loads.
I have tried countless different nginx configurations but none have worked. I have trawled the posts on here relating to the same issue but no joy. My last hope is to post my config file on here and hope that someone can help!
I followed this: https://gist.github.com/tegansnyder/96d1be1dd65852d3e576 tutorial to the letter and everything works great except the problem above.
Any suggestions would be GREATLY appreciated! I'm tearing my hair out here! Thanks in advance!
I have two config files:
FIRST CONFIG FILE
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;
}
SECOND CONFIG FILE
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
}
The problem was that my store was in /var/www/magento. Moved the files to just /var/www and all is good :) Thank for the help guys.
Please have a complete reading of this document: http://info.magento.com/rs/magentocommerce/images/MagentoECG-PoweringMagentowithNgnixandPHP-FPM.pdf where you can find a basic NGINX configuration specifically for Magento:
server {
listen 80 default;
# like ServerName in Apache
server_name magento.lan www.magento.lan;
# document root, path to directory with files
root /var/www/magento;
index index.html index.php;
# we don’t want users to see files in directories
autoindex off;
location ~ (^/(app/\|includes/\|lib/\|/pkginfo/\|var/\|report/config.
xml)\|/\.svn/\|/\.git/\|/.hta.+) {
#ensure sensitive files are not accessible
deny all;
}
location / {
# make index.php handle requests for /
try_files $uri $uri/ /index.php?$args;
# do not log access to static files
access_log off;
# cache static files aggressively
expires max;
}
location \~\* \.(jpeg\|jpg\|gif\|png\|css\|js\|ico\|swf)$ {
# look for static files in root directory
# and ask backend if not successful
try_files $uri $uri/ #proxy;
expires max;
access_log off;
}
location #proxy {
# proxy everything from this location to backend
fastcgi_pass fpm_backend;
}
location \~\.php$ {
# if reference to php executable is invalid return 404
try_files $uri =404;
# no need to cache php executable files
expires off;
fastcgi_read_timeout 600;
# proxy all requests for dynamic content to
fastcgi_pass fpm_backend;
# 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};
# Store code is defined in
# administration > Configuration > Manage Stores
fastcgi_param MAGE_RUN_CODE default;
fastcgi_param MAGE_RUN_TYPE store;
}
}
Related
I have deployed Nginx 1.9.10 and Laravel 8 on Cloud server with conf file as below:
server {
# Set the port to listen on and the server name
listen 80 default_server;
# Set the document root of the project
root /var/www/html/public;
# Set the directory index files
index index.php index.htm index.html;
# Specify the default character set
charset utf-8;
# Setup the default location configuration
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# Specify the details of favicon.ico
location = /favicon.ico { access_log off; log_not_found off; }
# Specify the details of robots.txt
location = /robots.txt { access_log off; log_not_found off; }
# Specify the logging configuration
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
sendfile off;
client_max_body_size 100m;
# Specify what happens when PHP files are requested
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param APPLICATION_ENV development;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
# Specify what happens what .ht files are requested
location ~ /\.ht {
deny all;
}
}
But the api is returning Content-Type as html even though i am returning json from laravel.
Checked .htaccess file as well everything is in place.
Laravel: code
Response::json([
"success" => true,
"message" => "Success message here",
]);
I had also faced a similar issue, there was an extra empty line above <?php in one of my file due to which it returned text/html response.
Removing that line fixed the issue.
Extra line
I need an nginx config that will work for both Laravel and Phpbb.
I have used laravel forge to setup my digital ocean server, and it created this nginx config:
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/djembefola.org/before/*;
server {
listen 80;
listen [::]:80;
server_name djembefola.org;
root /home/forge/djembefola.org/public;
# FORGE SSL (DO NOT REMOVE!)
# ssl_certificate;
# ssl_certificate_key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:$
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/dhparams.pem;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/djembefola.org/server/*;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/djembefola.org-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/djembefola.org/after/*;
The /public folder is where the front controller index.php of Laravel lives...
Also in the public folder, I have and install of phpbb at
- /public/board
I am upgrading the forum, and as such I need to rum the phpbb installer, which resides at:
localhost/board/install,
which then calls:
localhost/board/install/app.php/update
The above url is then giving a 404 error.
I have read elsewhere that this is because Nginx needs to be configured correctly in order to run the installer.
The sample Nginx config for phpbb is listed here.
So I need to merge these somehow, but so far my attempts have failed.
I tried adding :
location /board/ {
rewrite ^(.*)$ /app.php/$1 last;
}
to the existing laravel nginx file, but that fails. I am aware that I need to put it in the right place in the nginx config, but I fear I'm probably overlooking something else, as I am guessing a bit here...
Can anyone help please?
Based off the example config, you can nest required rules within a block wrap (just like they have done with the /install/ directory). eg:
location /board/ {
try_files $uri $uri/ #rewriteapp;
# Deny access to internal phpbb files.
location ~ /(config\.php|common\.php|cache|files|images/avatars/upload|includes|(?<!ext/)phpbb|store|vendor) {
deny all;
# deny was ignored before 0.8.40 for connections over IPv6.
# Use internal directive to prohibit access on older versions.
internal;
}
# Pass the php scripts to fastcgi server specified in upstream declaration.
location ~ \.php(/|$) {
# Unmodified fastcgi_params from nginx distribution.
include fastcgi_params;
# Necessary for php.
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
try_files $uri $uri/ /board/app.php$is_args$args;
fastcgi_pass php;
}
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
Note that #rewriteapp is outside the block wrap. Nginx will complain if you try to place it inside.
Just amend the paths to match your directory.
i am struggling to get my virtual host working with magento2.
Can anyone help?
server {
listen 80;
server_name local.magento2.com;
root /home/www/magento2/;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
index index.php;
try_files $uri $uri/ #handler; ## If missing pass the URI to Magento's front handler
expires off; ## Assume all files are cachable
}
location #handler { ## Magento uses a common front handler
rewrite / /index.php;
}
location /pub/static {
rewrite ^/pub/static/(.*)$ /pub/static.php?resource=$1? last;
}
# Foward php request to FastCGI server.
location ~ \.php$ {
#if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
fastcgi_pass php5-fpm-sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
Here is what seems to be an official magento2 nginx configuration.
upstream fpm_backend {
server 127.0.0.1:9000; # backend server:port address
}
server {
listen 80 default;
server_name magento.lan www.magento.lan;
root /var/www/magento2; # 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;
}
}
I can't access PHP files placed in sub-directories and my CSS/JS files return a 404 in browser. Yes, I have seen many others experiencing a 404 issue but their fixes don't seem to be my solution. I've also swapped out my configuration with several online. Here is my configuration:
server {
# Port that the web server will listen on.
listen 80;
# Host that will serve this project.
server_name .stackoverflow.com;
# Useful logs for debug.
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
rewrite_log on;
# The location of our projects public directory.
root /usr/share/nginx/html/public;
# Point index to the Laravel front controller.
index index.php;
location / {
# URLs to attempt, including pretty ones.
try_files $uri $uri/ /index.php?$query_string;
}
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
# PHP FPM configuration.
location ~* \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# We don't need .ht files with nginx.
location ~ /\.ht {
deny all;
}
# Set header expirations on per-project basis
location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {
expires 365d;
}
}
Is there something wrong with my configuration?
I am using Ubuntu 12.04 with NGINX and PHP5-FPM. I've just migrated my Joomla website from Apache to NGINX. But i think rewrite rules are not working well.
All of the SEF links rewrite to the homepage, but in the url bar link seems right.
Example if i click example.com/a/b.html it looks like go to the link in the url bar but homapage is loading.
Thanks for your help.
/etc/nginx/sites-available/example.com file
server {
listen 80;
server_name example.com;
#server_name_in_redirect off;
#access_log /var/log/nginx/localhost.access_log main;
#error_log /var/log/nginx/localhost.error_log info;
root /var/www/example.com/public_html/;
index index.php index.html index.htm default.html default.htm;
# Support Clean (aka Search Engine Friendly) URLs
location / {
try_files $uri $uri/ /index.php?q=$request_uri;
}
# deny running scripts inside writable directories
location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
return 403;
error_page 403 /403_error.html;
}
location ~ \.php$ {
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
#fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# caching of files
location ~* \.(ico|pdf|flv)$ {
expires 1y;
}
location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
expires 14d;
}
}
nginx.conf
user www-data;
worker_processes 8;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30;
types_hash_max_size 2048;
server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
gzip_vary on;
# gzip_proxied any;
gzip_comp_level 6;
# gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
## Block spammers and other unwanted visitors ##
include /etc/nginx/blockips.conf;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Yes - this reply is over 3 years old, but, unfortunately, the same problem exists on the latest version of Joomla when using NGINX...
We had the exact same problem on one of our client's sites. The problem turned out to be related to one of PHP's $_SERVER constants not being set by the NGINX server - and that constant (which is `$_SERVER['PHP_SELF']) is used in many places in Joomla to return the current URL. We have described, in details, the problem and how to fix it, here.
if you're using subfolder / virtual hosts, try changing:
# Support Clean (aka Search Engine Friendly) URLs
location / {
try_files $uri $uri/ /index.php?q=$request_uri;
}
To
# Support Clean (aka Search Engine Friendly) URLs
location /subfolder/ {
try_files $uri $uri/ /subfolder/index.php?q=$request_uri;
}
Hello this is my host configuration, wich is working fine with with Joomla 3.6.5 friendly URLs, Ubuntu 16.04 , PHP7 and nginx:
server {
listen 80;
server_name mydomain.com;
root /var/www/html/mydomain;
index index.php index.html index.htm default.html default.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
My solution was to make sure that this part of the php.ini is enabled: cgi.fix_pathinfo = 1. This fixed the problem that $_SERVER['PHP_SELF'] was not set.
I had it disabled by accident.
cgi.fix_pathinfo provides real PATH_INFO/PATH_TRANSLATED support for CGI. PHP's previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting of zero causes PHP to behave as before. Default is 1. You should fix your scripts to use SCRIPT_FILENAME rather than PATH_TRANSLATED. http://php.net/cgi.fix-pathinfo