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;
}
}
Related
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;
}
}
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?
We've setup 2 CodeIgniter apps which has its own complete code bases on its own directories as in this example. Plus we also have wordpress blog on the same public_html directory.
public_html/HOME_APP
public_html/admin_tool/ADMIN_TOOL_CODES
public_html/blog/WORDPRESS_CODES
HOME_APP codes (CodeIgniter) and our WORDPRESS works fine. But the admin_tool (CodeIgniter) doesn't work. We can access only the http://example.com/admin_tool/index.php but not any inside controller pages. When accessing its show 404 error page. And it looks like the routes are handled via APP1
The nginx rules are as follows. Appreciate if anyone can help us to fix the issue with admin_tool
server{
listen 80;
root /home/ubuntu/websites/example.com/public_html;
index index.html index.htm index.php;
server_name example.com;
access_log /home/ubuntu/websites/example.com/logs/access.log;
error_log /home/ubuntu/websites/example.com/logs/error.log error;
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
index index.php;
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ^~/admin_tool/ {
root /home/ubuntu/websites/example.com/public_html/admin_tool;
index index.php;
try_files $uri $uri/ /index.php$args;
}
}
server {
listen 443 ssl;
server_name example.com *.example.com;
return 301 http://$server_name$request_uri;
}
The following should do the trick for all of your installations:
# We define the index directory at the outermost level and therefore
# only once for all servers. Also note that we use the PHP file first
# because all main directories are handled by PHP scripts and this will
# give us best performance.
index index.php index.html index.htm;
server {
access_log /home/ubuntu/websites/example.com/logs/access.log;
error_log /home/ubuntu/websites/example.com/logs/error.log error;
# 80 is default!
#listen 80;
root /home/ubuntu/websites/example.com/public_html;
server_name example.com;
location / {
# Don't allow access to the logs directory.
location ~* ^/logs {
return 404;
}
# Don't allow access to PHP files.
location ~* \.php$ {
return 404;
}
# Handle static files.
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
# Directly return if the requested URI is a real file.
try_files $uri $uri/ index.php =404;
}
# Codeigniter and WordPress will always handle everything with their
# index.php script, therefore we only need to catch that case.
location = /index.php {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 443 ssl;
server_name example.com *.example.com;
return 301 http://$server_name$request_uri;
}
I had the same problem while I am using codeigniter in subfolder(dashboard).
All non-wordpress requests are not being entertained but remain on wordpress main page.
I fixed my issue by following lines in my configuration file /etc/nginx/sites-available/{my-site-name}
location /dashboard {
try_files $uri $uri/ /dashboard/index.php;
}
location /index.php {
fastcgi_pass unix:/usr/sbin/php5-fpm;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I have installed varnish on nginx. I have some really big problems.
my default.vcl is:
backend default {
.host = "127.0.0.1";
.port = "81";
}
my website virtual server is:
server {
listen 80;
root /var/www/site.com.ro/public_html;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name www.site.com.ro;
access_log /var/www/site.com.ro/logs/log.access;
error_log /var/www/site.com.ro/logs/log.error error;
location / {
index index.php;
try_files $uri $uri/ #handler;
}
location #handler {
rewrite / /index.php;
}
location /blog {
alias /var/www/site.com.ro/public_html_blog/;
index index.php index.html index.htm;
try_files $uri $uri/ /blog/index.php;
}
location ~ ^/blog(.+\.php)$ { ### This location block was the solution
alias /var/www/site.com.ro/public_html_blog/$1;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_read_timeout 300;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
if (!-e $request_filename) {
rewrite / /index.php last;
}
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_read_timeout 300;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 80;
# Make site accessible from http://localhost/
server_name site.com.ro;
rewrite ^(.*) http://www.site.com.ro$1 permanent;
}
and the varnish file is:
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"
when I try to change the listen port for the website to 81 I get a 500 error...
can anyone help me? I don't know what I did wrong.
Varnish is configured to listen to port 80, and connect to localhost:81. nginx is configured to listen to port 80. You didn't mention the intended flow, but I'm taking a wild shot and guessing:
client -> varnish:80 -> nginx:81.
Do you spot the problem now?
Hint:
server {
listen 80;
Oh, and make sure you have a real similar setup in a test machine (virtualbox - or something) that you use when you dabble with settings you're not familiar with. That will give you time to understand why something is not working, and will gain you invaluable experience so you don't have to mess up the production site(s).
Hi I have a magento store at mysite.com
Now I want to setup a url that will run my german website at mysite.com/german
Using the same install. I already have a half working config, but my issue is that beyond the homepage all magento URLs 404. Here is my current config.
server {
listen 80;
server_name mysite.com www.mysite.com;
####
#
# BELOW THIS LINE IS MY ATTEMPT TO GET mysite.com/german to run from the same directory as mysite.com
#
####
location ~ /german(.*)\.php($|/) {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www$1.php;
fastcgi_param MAGE_RUN_CODE german;
fastcgi_param MAGE_RUN_TYPE website;
}
location ~ /german(.*) {
index index.htm index.php;
autoindex off;
alias /usr/share/nginx/www$1;
}
location ~ /deutch/ {
index index.htm index.php;
try_files $uri $uri/ #handler;
}
The old config:
###
#
# THIS BIT I THINK IS THE PART WHICH IS NOT QUITE WORKING, BUT I DON'T KNOW WHAT THE #handler PART DOES
#
###
# This redirect is added so to use Magentos
# common front handler when handling incoming URLs.
location #handler {
rewrite / /index.php;
}
####
#
# BELOW THIS LINE IS THE ORIGINAL CONFIG WHICH RUNS mysite.com NO PROBLEMS
#
####
root /usr/share/nginx/www;
location / {
index index.htm index.php;
try_files $uri $uri/ #handler;
}
# 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 english;
fastcgi_param MAGE_RUN_TYPE website;
include fastcgi_params;
}
}
location ~* \.php$ {
if (!-e $request_filename) {
rewrite / /index.php last;
}
expires off;
set $runcode english;
set $runtype website;
if ( $request_uri ~* ^/german ) {
set $runcode german;
}
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 $runcode;
fastcgi_param MAGE_RUN_TYPE $runtype;
include fastcgi_params;
}
That is part 1, part 2 is to ensure the code is run from the same codebase.
One solution is to symlink the directory to the root directory, exec the following in the /usr/share/nginx/www directory:
ln -s . ./german
It's filty, but it works ;)
We're using the following to redirect to the correct index file:
location /german {
if ( -e $request_filename ) {
break;
}
rewrite ^/german(.*)$ /german/index.php last;
}
#
# Redirection of subdirectory php's to their respective php files
#
location ~ .php/ {
rewrite ^(.*.php)/ $1 last;
}
#
# Redirect everything else to root path
#
location / {
try_files $uri $uri/ /index.php?$args;
}