I have a Codeigniter PHP app, and I have configured my nginx conf to pretty up some of the URLs. For example, /index.php/home will rewrite to /home.
Now I am trying to configure nginx to rewrite http://mysite.com/sitemap.xml to /sitemap (which constructs the xml file). This is what I am using in my nginx conf:
if ($request_uri ~* ^/sitemap.xml)
{
rewrite ^/(.*)$ /index.php/sitemap last;
}
For some reason, the above doesn't work, I think another rule might be conflicting, but I can't figure out a workaround. Can anyone help? (full config file is below)
server {
listen 80;
root /www/myapp;
index index.php index.html index.htm;
access_log /var/log/nginx/access.log;
if (-f /www/myapp/application/errors/error_503.html)
{
return 503;
}
# canonicalize codeigniter url end points
#/home will redirect to /
if ($request_uri ~* ^(/home(/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;
}
############THIS PART IS NOT WORKING
if ($request_uri ~* ^/sitemap.xml)
{
rewrite ^/(.*)$ /index.php/sitemap last;
}
####################################
# 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;
}
## Default location
location / {
root /www/myapp;
index index.html index.htm index.php;
}
## Images and static content is treated different
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
expires 30d;
root /www/myapp;
}
## Parse all .php file in the www directory
location ~ .php$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /www/myapp/$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
## catch all
#error_page 404 /index.php;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
## Disable viewing .htaccess & .htpassword
location ~ /\.ht {
deny all;
}
}
Problem above is that REQUEST_URI remains as /sitemap.xml instead of /sitemap, so CI can't route it properly. Try changing "last" to "redirect":
if ($request_uri ~* ^/sitemap.xml)
{
rewrite ^/(.*)$ /sitemap redirect;
}
Also consider using LOCATION instead of IF (avoid IF in general):
location = /sitemap.xml
{
rewrite .* /sitemap redirect;
}
Hope that helps
I use it in on my website:
# Sitemap.xml
location /sitemap.xml {
alias /venvs/mobilepro/src/mobilepro/static/maps/map.xml;
}
Related
I am running magento under the Wordpress(Wordpress on the root and mangento under a subdirectory "/shop"). Previously I was running this under the Apache, So everything was running fine with separate .htaccess under the /shop folder.
As I move the server over NginX, all the internal magento URL showing 404. Please help me to solve this problem. Please suggest me how can I run the Magento under the /shop folder with Wordpress.
Here is my NginX Configuration file:
server {
listen 80;
server_name domain.com www.domain.com;
root /var/www/html;
index index.php index.html index.htm;
error_log /var/www/logs/error.log;
access_log /var/www/logs/access.log;
### gZip Setting ###
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;
### gZip End ###
location / {
if ($http_host !~ "^www\."){
rewrite ^(.*)$ http://www.$http_host/$1 redirect;
}
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location /shop/ {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
rewrite ^(.*.php)/ $1 last;
}
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
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
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
}
I got the solution, Here is my new configuration file:
server {
listen 80;
server_name domain.com www.domain.com;
root /var/www/html;
index index.php index.html index.htm;
error_log /var/www/logs/domain-error-ssl.log;
access_log /var/www/logs/domain-access-ssl.log;
### gZip Setting ###
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;
### gZip End ###
location / {
if ($http_host !~ "^www\."){
rewrite ^(.*)$ https://www.$http_host/$1 redirect;
}
try_files $uri $uri/ /index.php?$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
rewrite ^(.*.php)/ $1 last;
}
### Configuration for shop ###
location /shop {
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
if ($uri ~ "^/index.php/admin.*$"){
rewrite ^/index.php/admin(.*) /admin$1 redirect;
}
}
location ~ ^/shop/(app|includes|lib|media/downloadable|pkginfo|report/config.xml|var)/ { internal; }
location /shop/var/export/ { internal; }
location #handler { rewrite / /shop/index.php; }
### END Configuration for shop ###
location ~ .php$ { ## Execute PHP scripts
expires off; ## Do not cache dynamic content
fastcgi_read_timeout 120;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
#fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
}
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;
}
}
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;
}
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
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;
}