I'm migrating a Kohana App version 3.0.4 from Apache to NginX.
I'm getting troubles with the configuration .htaccess. On NginX work correctly only the internal URL's like:
http://myurlwithoutwww.net.ds/internalpage/anotherpage
but nether the homepage
http://myurlwithoutwww.net.ds/
nether this:
http://myurlwithoutwww.net.ds/index.php
works. The last two will simply display a blank screen.
this is the htaccess file from where I'm trying to migrate:
AddType text/x-component .htc
#Accept-Encoding: filter,deflate
# Turn on URL rewriting
RewriteEngine On
AddDefaultCharset UTF-8
# Installation directory
RewriteBase /
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+).([a-z]{2,3})$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# ExpiresActive On
# <FilesMatch \.(bmp|png|gif|jpe?g|html?|ico|doc|swf)$>
# ExpiresDefault "access plus 10 days"
# </FilesMatch>
FileETag none
# Protect application and system files from being viewed
RewriteRule ^async/?(.*)? index.php?dispatcher=async&$1 [NC,L,QSA]
RewriteRule ^(?:_application|_modules|_system)\b index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php?kohana_uri=$0 [PT,L,QSA]
#RewriteRule .* index.php/$0 [PT]
And this my current Nginx configuration:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
include /etc/nginx/aliases.conf;
root /var/www/webroot/ROOT;
index index.php;
location / {
expires off;
try_files $uri $uri/ #kohana;
}
# Prevent access to hidden files
location ~ /\. {
deny all;
}
location #kohana {
rewrite ^/(.+)$ /index.php$request_uri last;
}
location ~* \.php {
#location ~ /\. { deny all; access_log off; log_not_found off; }
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /var/www/webroot/ROOT/$fastcgi_script_name;
fastcgi_param KOHANA_ENV development;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT /var/www/webroot/ROOT;
fastcgi_index index.php;
}
try below config, also check bootstrap init if it has 'index_file' => FALSE.
server {
listen 80;
server_name *.domain.com;
client_max_body_size 20M;
access_log /home/user/public_html/site/log/access.log;
error_log /home/user/public_html/site/log/error.log;
root /home/user/public_html/site/public/;
index index.php;
location / {
# don’t check $uri/, send to php for nice error message
try_files $uri /index.php?$query_string;
}
location = /index.php {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
It is pretty old question, but maybe you will find it useful, the way I do:
location / {
index index.html index.htm index.php;
try_files $uri index.php;
}
location /kohana {
rewrite ^(.+)$ /kohana/index.php?kohana_uri=$1 last;
if (-f $request_filename) {
break;
}
if (!-e $request_filename) {
rewrite ^/kohana/(.+)$ /kohana/index.php?kohana_uri=$1 last;
}
}
I just listed the main sections of default.conf.
Related
I'm not a developer. I'm helping a friend to use a instance server at Amazon (Ubuntu) and I need a help to convert the .htaccess to nginx configs. I tried any sites that promise the automatic convertion, but without success. Can You help me in this case, please?
This is the .htaccess:
#http://# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
Thanks a lot
server {
listen 80;
server_name www.site.dev *.site.dev;
root /var/www/site/public_html/;
location / {
expires off;
try_files $uri $uri/ #kohana;
}
# Prevent access to hidden files
location ~ /\. {
deny all;
}
location #kohana {
rewrite ^/(.+)$ /index.php$request_uri last;
}
location ~* \.php {
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param KOHANA_ENV development;
fastcgi_cache off;
fastcgi_index index.php;
}
}
Source: https://github.com/primalskill/kohana-nginx-config
I'm currently using a rewrite to accept requests for files without including the .php extension, however, I can't find out how to have the requests with .php either be denied or redirected to the friendly version.
For example, this is what I want to accomplish:
/contact.php REDIRECT /contact
/contact.php (/contact.php exists, but only accessible via /contact) would result in a 403 error
Current config:
location / {
try_files $uri $uri/ #extensionless-php;
index index.php;
}
location ~ .php {
try_files $uri =404;
fastcgi_pass backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
Provided that what you have already works, I think what you're looking for is this part:
if ($request_uri ~ ^/(.*)\.php$) { return 301 /$1; }
Or, if you're using query parameters, too:
if ($request_uri ~ ^/([^?]*)\.php($|\?)) { return 301 /$1?$args; }
You should place it within your \.php$ location (which you should rename from your present .php). Trust me, it's NOT going to generate an infinite loop!
Some more explanation was provided in https://serverfault.com/questions/568569/nginx-url-rewrite-rule/568902#568902.
Hello I am trying to covert the following .htaccess lines so that arrowchat can work in my nginx server. The location of my arrowchat folder is like this /usr/share/nginx/html/arrowchat/ where /usr/share/nginx/html/ is the root folder for my website. My VPS has only one website, www.jukpac.com hosted on it. Below is the .htaccess code
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^chatroom ^/../public/chatroom/ [L]
RewriteRule ^cron ^/../public/cron/ [L]
RewriteRule ^debug ^/../public/debug/ [L]
RewriteRule ^list ^/../public/list/ [L]
RewriteRule ^mobile ^/../public/mobile/ [L]
RewriteRule ^popout ^/../public/popout/ [L]
RewriteRule ^video ^/../public/video/ [L]
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\.(gif|jpg|png|css|swf)$">
Header add "Expires" "Mon, 28 Jul 2014 23:30:00 GMT"
Header add "Cache-Control" "max-age=31536000"
</FilesMatch>
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A604800
ExpiresByType text/css A604800
ExpiresByType image/gif A604800
ExpiresByType image/png A604800
ExpiresByType image/jpeg A604800
ExpiresByType application/x-shockwave-flash A604800
</IfModule>
I tried a few online .htaccess converter but it didn't work can some one please let me know how to fix this?
My current default.conf file for nginx looks like this
#
# The default server
#
server {
listen 80;
server_name jukpac.com;
return 301 http://www.jukpac.com$request_uri;
}
server {
listen 80;
server_name www.jukpac.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/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/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
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 .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Ok with the Help of a friend, in Serverfault I managed to get the correct configuration as shown below
server { #redirecting server
listen 80;
server_name jukpac.com;
return 301 http://www.jukpac.com$request_uri;
}
server {
listen 80;
server_name www.jukpac.com;
root /usr/share/nginx/html; #move the root to server level
index index.php # move index to server level
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location / {
try_files $uri $uri/ =404;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png|swf)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
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;
}
location ~ /\.ht {
deny all;
}
}
I want to make Zend Framework 2 application to run within Nginx server. I could access the homepage, but I could not access any other modules. I found that Nginx needs a rewrite rule to access the URL such as domain/album. The question is how to convert the default .htaccess rules of Zend Framework 2 to Nginx rewrite rule?
Here are the .htaccess rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
You really do not need any rewrite rules to make Nginx and ZF2 to play nice together. Here is a very simple Nginx configuration which I use:
server {
listen *:80;
server_name DOMAIN;
# Character Set
charset utf-8;
# Logs
access_log /vhosts/DOMAIN/logs/access_log main;
error_log /vhosts/DOMAIN/logs/error_log;
# Directory Indexes
index index.php;
# Document Root
root /vhosts/DOMAIN/public;
# Location
location / {
try_files $uri $uri/ /index.php;
}
# Error Pages
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
# PHP-FPM Support
location ~ \.php$ {
fastcgi_pass unix:/usr/local/etc/php-fpm/DOMAIN.sock;
include fastcgi.conf;
}
# Block access to .htaccess
location ~ \.htaccess {
deny all;
}
}
Of course change the paths to your current setup. Since you did not mention what type of PHP installation you are using I can't help you there because I am currently using PHP-FPM.
Using this very simple setup all my modules are working as expected. For example I could visit http://example.com/some/url OR http://example.com/index.php/some/url
Nginx also has a simple configuration for ZF http://wiki.nginx.org/Zend_Framework#Time_for_nginx
Edit 1 - Added fastcgi_params config
fastcgi_param SCRIPT_FILENAME $document_root$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_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
To follow up on Diemuzi's answer.
Please remember, that if you want query parameters to be working in your application, you have to pass them along with try_files
Best way i found is to use both $is_args and $args. $is_args sets only a question mark if there is arguments, and $args pass the query arguments.
Replace
try_files $uri $uri/ /index.php;
with
try_files $uri $uri/ /index.php$is_args$args;
Now you can use query params as normally in your ZF2 application for example in a default controller
$query_params = $this->params()->fromQuery();
URL rewrite is not working in Nginx and operating system is Ubuntu 12.4 Lts
when open http://mvc.loc it is working
but when i try to open http://mvc.loc/login Not working
404 Not Found
nginx/1.1.19
.htaccess
<IfModule !mod_rewrite.c>
ErrorDocument 500 "mod_rewrite must be enabled"
</IfModule>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?u=$1
virtual hosts for mvc.loc
server {
listen 80;
server_name mvc.loc;
access_log /var/log/nginx/mvc.loc.access.log;
error_log /var/log/nginx/mvc.loc.error.log;
root /usr/share/nginx/www/mvc;
index index.php;
# use fastcgi for all php files
# Are you sure you have this set up?
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;
}
}
location / {
rewrite ^(.*)$ /index.php?u=$1 last;
}
Well #NanheKumar's answer got the rewrite correct but it ignored the first 2 rules in the htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
This means check if the request doesn't match a file and doesn't match a directory, to imitate this exact behavior you can use try_files like this
location / {
try_files $uri $uri/ /index.php?u=$request_uri;
}
This will make sure to serve requests that point to an asset or a directory directly first then if neither it would pass the request to index.php
EDIT: unless index.php is able to serve assets, this will cause all assets ( images, css, javascript, etc ) to show errors because index.php will be receiving arguments it's not expecting. Think about something like this http://example.com/index.php?u=/images/background.jpg