Whenever I put a known file extension in the url nginx return 404 Not Found.
domain.com/myroute.foo and domain.com/foo/myroute.foo is fine, but domain.com/myroute.php and domain.com/foo/myroute.php (or for example .css, .js) returns 404 Not Found.
My nginx server config:
server {
listen 80;
server_name domain.com;
root /var/www/path/public;
charset utf-8;
gzip on;
#access_log logs/host.access.log main;
location / {
index index.html index.php index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
add_header Cache-Control public;
add_header Cache-Control must-revalidate;
expires 7d;
access_log off;
}
}
Why isn't a url with a known file extension (/myroute.php) going to my index.php file like any other url?
myroute.php does not exist on your server.
Nginx location directives are checked in this order
Directives with the "=" prefix that match the query exactly (literal string). If found, searching stops.
All remaining directives with conventional strings. If this match used the "^~" prefix, searching stops.
Regular expressions, in the order they are defined in the configuration file.
If #3 yielded a match, that result is used. Otherwise, the match from #2 is used
That means that your myroute.php request gets handled by the ~ \.php$ location block, which results in a 404 as per your try_files directive.
To solve this, you'll need to either make your location directive more specific (e.g. ~ index\.php$), or try_files exactly as you do in the location /. Using a rewrite could also solve your issue.
EDIT:
It is important to understand in what order nginx chooses location blocks over other location blocks. See more at the nginx wiki
Regarding your question, the easiest solution I think is to use try_files
try_files $uri $uri/ /index.php?q=$uri&$args;
in both your location ~ \.php$ { and location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) { blocks
Note: don't forget to remove your old try_files $uri =404 from the .php$ block
Your final conf file should now look like this
server {
listen 80;
server_name domain.com;
root /var/www/path/public;
charset utf-8;
gzip on;
#access_log logs/host.access.log main;
location / {
index index.html index.php index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?q=$uri&$args;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
try_files $uri $uri/ /index.php?q=$uri&$args;
add_header Cache-Control public;
add_header Cache-Control must-revalidate;
expires 7d;
access_log off;
}
}
Related
i want to create routing like a:
hxxp://127.0.0.1/ <-- default with default location /var/www/ without listning directories
hxxp://127.0.0.1/allegro/
How to do it?
If i go to hxxp://127.0.0.1/allegro/scripts/test.php i see a blank page. If i go to hxxp://127.0.0.1/ php scripts excutes normally and i see phpinfo()
My nginx config:
server {
listen 8000 default_server;
listen [::]:8000 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php7.0-fpm.sock;
}
location /allegro/ {
alias /var/www/allegro/;
autoindex on;
location ~ \.php$ {
fastcgi_split_path_info ^(.+?\.php)(/.*)?$;
fastcgi_pass unix:/run/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
}
There are multiple issues with your current configuration. The most important is that the URI /allegro/scripts/test.php is not processed by the nested location block, because regular expression location blocks take precedence (unless a ^~ modifier is used).
location ^~ /allegro/ {
root /var/www;
# autoindex on;
try_files $uri $uri/ =404;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php7.0-fpm.sock;
include fastcgi_params;
}
}
The ^~ modifier ensures that this prefix location takes precedence over regular expression locations at the same level. See this document for details.
Note that the root statement is preferred over an alias statement where applicable (see this document for details).
The fastcgi_split_path_info and fastcgi_index directives are unnecessary for location blocks which do not match URIs with path info.
You will need to decide whether include fastcgi_params or include snippets/fastcgi-php.conf is the more appropriate source for FastCGI parameters.
Laravel is not receiving any $_GET variables from the URL query string. The $_GET and Input::all() are empty.
Example:
example.app/ex/login.php?country=US
The "country=US" never shows up in my $_GET variable
After much research and trying many different NGINX configurations, I can now only produce results when this example is used.
Example:
example.app/index.php/ex/login.php?country=US
The $_GET variable now shows the country name value pair.
What is the proper configuration to allow query strings within the URL?
My current sites-enabled configuration for my "example.app" is...
server {
listen 80;
server_name example.app;
root /home/vagrant/Code/public;
index index.html index.htm index.php;
charset utf-8;
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/registration.app-error.log error;
error_page 404 /index.php;
sendfile off;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
}
This appears correct, aside from being commented out.
location / {
#try_files $uri $uri/ /index.php?$query_string;
}
The bare bones one I use in testing is:
server {
listen 80 default_server;
root /var/www/project/public;
index index.php
server_name localhost;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
}
$query_string and $args are functionally identical according to:
NGINX Docs
This is the sites-enabled NGINX server configuration that ended up working for me...
server {
listen 80;
server_name registration.app;
root /home/vagrant/Code/registration/public;
charset utf-8;
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/registration.app-error.log error;
error_page 404 /index.php;
sendfile off;
# Point index to the Laravel front controller.
index index.php;
location / {
try_files $uri $uri/ index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.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;
}
location ~ /\.ht {
#deny all;
}
}
For me it worked only by changing
try_files $uri $uri/ /index.php?$query_string; to
try_files $uri $uri/ /index.php?$args inside location / block on ubuntu (14.04) nginx
My NGINX congfiguration was something like this:-
From
location / {
try_files $uri $uri/ /index.php?is_args$args;
}
To
location / {
try_files $uri $uri/ /index.php?$args;
}
I just remove the is_args from try_files and working fine.
I have a perfect configuration which is nginx, php5-fpm, apc, varnish and mariadb. Everything works flawless except;
I am hosting a single web site, since my server resources are high and available, I want to host other web sites on the same server. When I try to add different websites into nginx the service simply does not restart.
here's my configuration file when everything works:
server {
listen 8080;
root /usr/share/nginx/www;
index index.php index.html index.htm;
server_name www.domain1.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
I don't want to use seperate files for different virtual hosts, I want to do everything in default file. But when I add another virtual host like below and save default file. nginx won't restart.
server {
listen 8080;
root /usr/share/nginx/www;
index index.php index.html index.htm;
server_name www.domain1.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
server {
listen 8080;
root /usr/share/nginx/domain2;
index index.php index.html index.htm;
server_name www.domain2.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
Please help me resolve this issue. I think something is conflicting but don't know what.
ok I found the solution just by investigating error log.
2014/08/19 21:55:07 [emerg] 5927#0: could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
error log tells me to increase hash bucket size..
I edited nginx.conf and set the bucket size to 32 as advised in error log, it didn't work at first, but then I set it to 64 and it worked.
just search for "bucket" in nginx.conf, uncomment it, then set to 64 (or above in some cases) it will work, unless there is another issue.
I have this directive in my nginx virtual host file:
try_files $uri $uri/ /profile.php?nickname=$uri&$args;
It works ok, but I want to rewrite only 0-9a-z- characters so that users have nice profile addresses, for example:
http://www/mydomain.com/john
But nginx rewrites everything. For example, if I enter address:
http://www.mydomain.com/non-existing-holder/nonexisting-filename.html
instead of returning 404 error it rewrites everything.
Is there any way how to rewrite only 0-9-a-z- characters?
server {
listen 80;
server_name www.mydomain.com;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
root /var/www/mydomain;
error_page 404 /4044.html;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /profile.php?nickname=$uri&$args;
rewrite ^/forum/([0-9-]+)/([0-9a-z-]+).html$ /forum.php?tid=$1&title=$2 last;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
You can try the following configuration.
Request for /testUSER would return a 404
Request for /forum/100/something.html would go to /forum.php?tid=100&title=something
Request for /nickname would go to /profile.php?nickname=nickname
.
location / {
index index.html index.htm index.php;
rewrite ^/([0-9a-z-]+)/?$ /profile.php?nickname=$1&$args last;
try_files $uri $uri/ =404;
}
location /forum {
rewrite ^/forum/([0-9-]+)/([0-9a-z-]+).html$ /forum.php?tid=$1&title=$2 last;
alias /var/www/mydomain/forum;
}
I'm using Wordpress as root of my website and Invision Power Boards as forum.
http://localhost -> Wordpress
http://localhost/forum -> IPB
I have removed "index.php" from Wordpress URLs successfully with Nginx-rewrite however when I try to use SEO Friendly URLs on IPB, nginx simply returns to Wordpress' 404 page.
My configuration is like this:
#This removes "index.php" from Wordpress URLs
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
Then I follow this link to modify my nginx conf file in order to be able to use SEO friendly URLs of IPB: http://www.devcu.com/forums/topic/262-furl-friendly-urls-with-ipb-and-nginx/
#This part is to be able to use IPB SEO
location /forum/ {
index index.php;
try_files $uri $uri/ /forum/index.php?$uri&$args;
rewrite ^ /index.php? last;
}
When I click a link on my forum (for example: http://localhost/forum/index.php/forum/51-sport/) nginx simply redirects me to (http://localhost/forum/forum/51-sport/) which displays Wordpress 404 error page.
I have very little knowledge about regex so any help would be appreciated.
This is my whole conf file after modifications, little messy I accept that.
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /home/user_name/public_html;
access_log /var/log/nginx/a/access.log;
error_log /var/log/nginx/a/error.log
server_name localhost;
server_tokens off;
location / {
try_files $uri $uri/ #wordpress;
}
location #wordpress {
fastcgi_pass php-fpm;
fastcgi_param SCRIPT_FILENAME /home/user_name/public_html$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME /index.php;
}
location /forum {
try_files $uri $uri/ try_files $uri $uri/ /forum/index.php?q=$uri;
}
location /forum/ {
try_files $uri $uri/ try_files $uri $uri/ /forum/index.php?q=$uri;
}
#location / {
#index index.php index.html index.htm;
#try_files $uri $uri/ /index.php?q=$uri&$args;
#}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ \.php$ {
fastcgi_split_path_info ^(/)(/.*)$;
}
# Add trailing slash to */wp-admin and */forum requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9001
#location ~ \.php$ {
# fastcgi_split_path_info ^(/)(/.*)$;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /home/user_name/public_html$fastcgi_script_name;
# fastcgi_param PATH_INFO $fastcgi_script_name;
# include /etc/nginx/fastcgi_params;
#REMOVE THIS
#fastcgi_read_timeout 60000;
#fastcgi_send_timeout 6000;
#}
}
Since the last post, I have played with IPB's SEO configurations and I managed to remove "index.php" from URLs. It doesn't effect the result of course. But it seems that location / decides what to do and therefore link is being considered as a Wordpress permalink.
EDIT - Solution
# Upstream to abstract backend connection(s) for php
upstream php {
# server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9001;
}
server {
## Your website name goes here.
server_name localhost;
## Your only path reference.
root /home/username/public_html;
## This should be in your http block and if it is, it's not needed here.
index index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# This is cool because no php is touched for static content
try_files $uri $uri/ /index.php;
}
location /forum {
try_files $uri $uri/ /forum/index.php;
rewrite ^ /forum/index.php? break;
}
location ~ ^/forum/index.php {
if ($args != "") {
rewrite ^ http://www.google.com/ permanent;
}
try_files $uri $uri/ /forum/index.php;
rewrite ^ /forum/index.php? last;
}
location /forum/admin/ {
try_files $uri $uri/ /forum/admin/index.php;
rewrite ^ /forum/admin/index.php? last;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include /etc/nginx/fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
I'm too using wordpress and ipb on nginx, now configuring, i added wordpress permalink to ipb config and turn on seo Rewrite URLs, Force Friendly URLs
location / {
try_files $uri $uri/ /index.php?$args;
}
it worked for me