Removing index.php from Magento URL While Using Nginx - magento

My current setup is running Ubuntu 12.04 LTS, Nginx 1.4.1, with Magento 1.7.0.2.
Earlier today, I set up a basic Magento store. I haven't installed any modules and I haven't made any configuration changes. I'm just looking to get the groundwork laid before delving further into the system. I am looking to remove the "index.php" that appears between the domain name and the file path.
My vhost is taken from the nginx website:
server {
root /path/to/root/domain/html;
index index.php;
error_log /path/to/error/log/error.log;
access_log /path/to/transfer/log/transfer.log;
server_name servername.com;
location / {
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
}
location ^~ /(app|includes|lib|media/downloadable|pkginfo|report/config.xml|var)/ { internal; }
location /var/export/ { internal; }
location /. { return 404; }
location #handler { rewrite / /index.php; }
location ~* .php/ { rewrite ^(.*.php)/ $1 last; }
location ~* .php$ {
if (!-e $request_filename) { rewrite / /index.php last; }
expires off;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param MAGE_RUN_CODE default;
fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params;
}
}
I see that other people have have been searching for the answer to this question, there are code snippets to solutions, but none of these appear to work with my system. When I make changes to my Nginx vhost, I always restart nginx, but index.php still shows up in the URL.

Nginx config looks fine. In system > configuration > general > web in your admin panel set Use Web Server Rewrites to yes and clear the cache in magento.

Add this to magento nginx vhost.
location / {
if (!-e $request_filename) {
rewrite ^/([^?]*)(?:\?(.*))? /index.php?title=$1&$2 last;
}
if ($uri ~* "\.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$") {
expires max;
break;
}
}

Related

Laravel + NGINX CSS/JS 404 not found

Laravel 5.5 fresh
nginx version: nginx/1.10.3 (Ubuntu)
When I go to: http://<my-ip>/crm/ all is working great, I get the Laravel welcome page, all js and css are loading correctly.
When I go to http://<my-ip>/crm/register - I get 404 for css and js.
This is my conf:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name _;
rewrite ^/((?!en)[a-z]*)/home$ /index.php?lang=$1&$args last;
rewrite ^/((?!en)[a-z]*)/sitemap.xml$ /sitemap.xml last;
rewrite /pigeon/(.*)$ /pigeon/index.php?/ last;
rewrite /crm/(.*)$ /crm/index.php?/ last;
if (!-f $request_filename){
set $rule_52 1$rule_52;
}
if ($rule_52 = "1"){
rewrite ^/(.*)(?<!php)$ /$1.php last;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /pigeon {
alias /var/www/html/pigeon/public;
try_files $uri $uri/ #pigeon;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
location /crm {
alias /var/www/html/crm/public;
try_files $uri $uri/ /index.php?$query_string;
location /crm.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
How do I fix this conf file to catch css and js files correctly?
Thanks
I had a similar issue. It seems allright what you have there in the server block.Although you might be better off if you add a proper server name.
Check this question and see if it helps. It has to do with the symlink to var/www/html for your crm, if you used one.
Laravel 8 + nginx - app.css and app.js resources from public/ not loading - 404 not found
And if not you could still use the online CDN's from bootstrap.Also in the question.

Fresh Magento 2 installation only shows HTML content without styling

I just completed a fresh Magento 2 installation without any errors, but when I go to home page or /admin page I only see HTML content without any styling.
Any idea why? I use the same nginx config for Magento 1 which worked fine.
PHP 7.0.8
nginx/1.10.0
MySQL 5.7.16
Home page:
Console errors:
nginx config:
server {
root /var/www/html/magento2/;
index index.php;
server_name magento2.dev;
location / {
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
}
location ~ ^/(app|includes|lib|media/downloadable|pkginfo|report/config.xml|var)/ { internal; }
location /var/export/ { internal; }
location /. { return 404; }
location #handler { rewrite / /index.php; }
location ~* .php/ { rewrite ^(.*.php)/ $1 last; }
location ~* .php$ {
if (!-e $request_filename) { rewrite / /index.php last; }
expires off;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param MAGE_RUN_CODE default;
fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params;
}
}
EDIT:
After messing around with it now when I go to /admin page I get this:
There has been an error processing your request
Exception printing is disabled by default for security reasons.
Error log record number: 846002728319
You should deploy static content by running php bin/magento setup:static-content:deploy.
I believe that this step is in installation instruction of Magento.
Also you should make sure that your files have correct ownership and permissions so web server can access them. To make sure that everything is correct (if your web server is running as www-data) open your Magento directory in console and type:
chown www-data:www-data -R *
That should change ownership of all files to web server.

Nginx/PHP-FPM: Returning 404s for CSS and JS

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?

Magento 1.4 - nginx server | 302's to Permanent 301's?

we have an older version of Magento (1.4) running on an nginx server. We have a few temporary redirects (302's) on the site that need to be converted to permanent 301 redirects.. There is no way to do this in 1.4 by means of the Administraton Panel :(
Can anyone provide me with the nginx configuration directives to do this?
Here is my config file for the domain:
rewrite ^/minify/([0-9]+)(/.*.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;
rewrite ^/skin/m/([0-9]+)(/.*.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ #handler; ## If missing pass the URI to Magento's front handler
expires 30d; ## Assume all files are cachable
}
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 /lib/minify/ {
allow 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)(.*) {
if (!-e $request_filename) { rewrite ^(.*)$ /index.php break; }
## Catch 404s that try_files miss
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param HTTPS $fastcgi_https;
#fastcgi_param MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
#fastcgi_param MAGE_RUN_TYPE store;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/luxuryleathergoods.com/public_html$fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
}
}
For each redirect, you can add the following line to your nginx configuration, inside of the location / block (first):
rewrite /old/url.html /new/url.html

nginx configuration, avoid codeigniter rewrite rules

This is nginx rewrite rule for codeigniter.
We can find this easily with googling.
server
{
server_name .example.com;
access_log /var/log/nginx/example.com.access.log;
root /var/www/example.com/html;
index index.php index.html index.htm;
# enforce www (exclude certain subdomains)
# if ($host !~* ^(www|subdomain))
# {
# rewrite ^/(.*)$ $scheme://www.$host/$1 permanent;
# }
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# removes access to "system" folder, also allows a "System.php" controller
if ($request_uri ~* ^/system)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# catch all
error_page 404 /index.php;
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/html$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /\.ht
{
deny all;
}
}
I want to add normal php project in this server.
The project's root directory is /var/www/another/proj_new.
As I mentioned, this new project do not use codeigniter frame.
This is normal php file project. So it doesn't need Codeigniter rewrite rule.
So, my question is is possible that I can access the new project through the web.
the address may be like this:
http://example.com/proj_new
This address should not call codeigniter's proj_new controller.
I've tried to add this setting :
server
{
....
....
....
localhost /proj_new {
root /var/www/another/proj_new
index index.php
}
....
....
....
}
but, http://example.com/proj_new
makes 404 error pages.
I suggest this configuration from Nginx
server {
server_name nginxcodeigniter.net;
root /var/www/codeigniter;
index index.html index.php;
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
After this, make sure that your codeIgniter config.php contains the following information:
$config['base_url'] = "http://nginxcodeigniter.net/";
$config['index_page'] = "";
$config['uri_protocol'] = "REQUEST_URI";
Source: Nginx
The !-e in the following section of code means that if the file, directory or symlink does not exist, redirect to use the rewrite rule. The fact that you have this present should be enough for you to just create a folder proj_new and the rewrite rule should be ignored.
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
I presume you have tried just creating the proj_new folder already? It looks to me as if you already have sufficient means to achieve what you want in your file and I can't see any errors with it. You are creating your proj_new folder inside the html folder, right?
Just had a play about with this and it works fine. Your configuration works as expected. Attached below is my nginx.conf file so you can have a look. This was CI2.1, Nginx 1.0.1 Stable, Windows 7, PHP 5.3.1.
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
index index.php index.html index.htm;
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# removes access to "system" folder, also allows a "System.php" controller
if ($request_uri ~* ^/system)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# catch all
error_page 404 /index.php;
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /\.ht
{
deny all;
}
}
}
What version of nginx are you using? This should work on newer versions, with the try_files directive.
http://ericlbarnes.com/post/12197460552/codeigniter-nginx-virtual-host
server {
server_name .mysecondsite.com;
root /sites/secondpath/www;
index index.html index.php index.htm;
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
s// try this change the "backend" with your projectname
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
# canonicalize url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
{
rewrite ^/backend/(.*)$ /backend/ permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/backend/(.*)/index/?$ /backend/$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/backend/(.+)/$ /backend/$1 permanent;
}
# removes access to "system" folder
if ($request_uri ~* ^/system)
{
rewrite ^/backend/(.*)$ /backend/index.php?/$1 last;
break;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/backend/(.*)$ /backend/index.php?/$1 last;
break;
}
can you add:
Can access to "backend" folder
if ($request_uri ~* ^/backend)
{
rewrite ^/(.*)$ /backend/index.php?/$1 last;
break;
}

Resources