my old server had this in the htaccess file:
< FilesMatch "^resort$">
ForceType application/x-httpd-php
< /FilesMatch>
where I had a resort php file (without the php extension)...
so the file was domain.com/resort/param1/param2
I'm struggling to make the equivalent work for nginx...
i've tried these items, but none work:
location = /resort/ {
try_files $uri /resort.php;
try_files $uri $uri/ #extensionless-php;
try_files $uri $uri/ $uri.html $uri.php?$query_string;
try_files $uri $uri/ /resort.php$is_args$args;
rewrite ^(.*)$ /resort.php last;
}
So how do I execute the resort file as php, when this url is in the browser:
domain.com/resort/param1/param2.php
THANKS!
PS. would love some pages/resources/tutorials that explain "apache to nginx" for people who don't understand nginx :)
i've been to nginx site, but IMO, I need to know more than I do to figure it out or understand what the nginx site is saying.
update:
i think this is close, but still not working :(
this is url: domain.com/resort/city/state.php
here is directive:
location ~ /resort/ {
rewrite ^/resort/(.*)/(.*) /resort/$1/$2 break;
}
this is what worked for me:
location ^~ /resort/ {
try_files $uri $uri/ /resort.php; }
To execute a file without a .php extension as though it was a PHP file, you will need to replicate the location block that handles those types of request.
It will look something like this:
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass ...;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
To process the URI /resort/param1/param2 using the PHP file located at /resort, you might use something like:
location ~ ^/resort/ {
include fastcgi_params;
fastcgi_pass ...;
fastcgi_param SCRIPT_FILENAME $document_root/resort;
}
See this document for location syntax.
Related
I'm a new Laravel learner. On my Mac (macOS 10.13), I have Nginx, PHP, and MySQL environment configured. At first, the Nginx localhost:8080/laravel/public displays the laravel welcome page without any issue. But when I try to add custom routes such as:
Route::get('test', function () {
return 'Hello World!';
});
I got 404 page on localhost:8080/laravel/public/test.
After I Googled the solution for route 404 issue, I modified my nginx conf file by adding
try_files $uri $uri/ /index.php?$query_string
as below:
server {
...
location / {
try_files $uri $uri/ /index.php?$query_string;
}
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;
}
}
Now is the story, the 404 issue has be fixed. But I still cannot access the correct route page.
When I open localhost:8080/laravel/public/test again, the browser takes me to the nginx docroot page (that is, localhost:8080/index.php).
I have tried php artisan serve in my laravel home folder, using that command localhost:8000/test can be accessed correctly with "Hello World!" text.
Updated:
I just tried some other strings after localhost:8080/, such as localhost:8080/abc or something, it seems any subpath will takes me to the localhost:8080/index.php page! after try_files $uri $uri/ /index.php?$query_string was added. If I remove this line of code, localhost:8080/abc will show 404 page (which should be correct because I don't have abc.php file in the root folder).
It seems like after adding
location / {
try_files $uri $uri/ /index.php?$query_string;
}
Nginx redirects all the url it not recognized to the default homepage?
Can someone tell me why my Nginx cannot work with Laravel route after try_files $uri $uri/ /index.php?$query_string is added?
If you need more detail, please let me know. Thanks very much!
Well.. I solved this issue by modifying my Nginx site config file, changing
server {
...
root /var/www;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
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;
}
}
to
server {
...
root /var/www/laravel/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
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;
}
}
Yes, just change the root path to laravel's public folder then restart nginx server.
Now I got Hello World! on http://localhost:8008/test
I am running CodeIgniter 3.0.6 in an Nginx server and subpaths end up serving /index.php, rather than /<installdir>/index.php. So, if I ask for /CodeIgniter-3.0.6/home/ I get served the /index.php page instead, rather than /CodeIgniter-3.0.6/index.php as expected. Note, that my CodeIgniter application will eventually reside in /2016/.
I suspect this is down to a misconfiguration of my Nginx install, rather than something CodeIgniter related? My Nginx install is running on Ubuntu 16.04. The contents of the /etc/nginx/sites-enabled/default are:
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi.conf;
}
Is there something else I should be changing?
If your server has a single application with the main entry point at /CodeIgniter-3.0.6/index.php, you should probably set your locations like this:
location / {
try_files $uri $uri/ /CodeIgniter-3.0.6/index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi.conf;
}
That way, any URI that is not a match for a resource file or a .php file, will be routed to your applications default entry point /CodeIgniter-3.0.6/index.php.
However, if there is more than one application, such that /index.php and /CodeIgniter-3.0.6/index.php are two distinct entry points, you may want to set up locations for each application, possibly like this:
location / {
try_files $uri $uri/ /index.php;
}
location /CodeIgniter-3.0.6 {
try_files $uri $uri/ /CodeIgniter-3.0.6/index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi.conf;
}
That way, only URIs that begin with /CodeIgniter-3.0.6 will be routed to that application's default entry point.
I'm using nginx with PHP-FPM on a ISPConfig3 server.
I put the following rewrite-rule in my nginx-directives (to make prettier links in Pydio):
location ~ \.php$ {
try_files #php;
}
location #php {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9026;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
proxy_set_header X-Accel-Mapping /var/www/XXXYYY.com/pydio/data/=/data/;
location /conf/ { deny all; }
location /data/ { internal; }
location /data/public/ {
try_files $uri $uri.php =404 last;
}
I want URLs in pydio.XXXYYY.com/data/public/* to have a .php-extension added.
This rule finds the files without the .php in the address bar but now they are downloaded instead of executed.
Since I use ISPConfig3, the rewrites for .php-files (to have them executed by PHP-FPM) is above the stated part. But I thought adding "last" should take care of that.
What else could I try?
Thank you!
First of all, you misunderstand the try_files directive. There's no "last" argument and it doesn't work like you think. Please, check the documentation: http://nginx.org/r/try_files. It's technical documentation, read it literally, every word has meaning.
To solve your problem you have to remove two last arguments from try_files:
try_files $uri $uri.php =404 last;
should be replaced with:
try_files $uri $uri.php;
I'm trying to redirect links that look like:
http://example.com/dev/some_project
to their physical location:
http://example.com/dev/some_project/some_project.php
In order to achieve this, I came up with this following rule set:
location / {
try_files $uri $uri/ #folderless-php;
index index.php index.html index.htm;
}
location #folderless-php {
rewrite ^(.*)$ "${uri}/${basename}.php";
}
This, however, will for some reason just invoke an internal server error.
So I tried changing it to:
location / {
set $folderless "${uri}/${basename}.php";
try_files $uri $uri/ $folderless;
index index.php index.html index.htm;
}
This seemed to work with curl, but when I tried this in any browser, I was just offered to download the file that I tried to access, to my astonishment.
What causes this behavior? Is there a way to achieve what I'm trying to do?
You are being given a download as you haven't told Nginx how to handle PHP files. You need to set up the fast-cgi parameters to something like:
location ~ \.php$ {
fastcgi_read_timeout 60000;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/site$fastcgi_script_name;
include fastcgi_params;
}
I want my nginx make display all url's clean.
http://www.mydomain.com/indexhtml.html as http://www.mydomain.com/indexhtml
http://www.mydomain.com/indexphp.php as http://www.mydomain.com/indexphp
With some research I've made the first case to work. It`s done by following configuration:
location / {
root html;
index index.html index.htm index.php;
try_files $uri.html $uri/ =404;
}
It works for indexhtml.html displaying as indexhtml, but nothing happens with .php. If I change $uri.html to $uri.php, it works neither for .html, neither .php. I`ve tried to put something similar in php location but without any success.
Any advices?
From what I've researched, if you append your /etc/nginx/conf.d/domain.tld.conf file to include:
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
Then restart nginx and give it a go. Hopefully this will help you! More information can be found (where I found it) here # tweaktalk.net
No need for extra blocks and named locations and everything. Also move the index line outside the location block
server {
index index.html index.php;
location / {
try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
}
location ~ \.php$ {
try_files $uri =404;
# add fastcgi_pass line here, depending if you use socket or port
}
}
Keep in mind that if you have a folder and a file with the same name inside the same folder, like /folder/xyz/ and /folder/xyz.php you won't be able to run the php file if the folder xyz contains an index.php or index.html, just keep this in mind.
To further Mohammad's answer, you might also want to offer redirects from .html and .php to the extensionless versions.
This can be accomplished due to the fact that $request_uri contains "full original request URI (with arguments)", and is not affected by the internal rewrites that are not visible to the user.
server {
index index.html index.php;
location / {
if ($request_uri ~ ^/(.*)\.html$) { return 302 /$1; }
try_files $uri $uri/ $uri.html $uri.php?$args;
}
location ~ \.php$ {
if ($request_uri ~ ^/([^?]*)\.php($|\?)) { return 302 /$1?$args; }
try_files $uri =404;
# add fastcgi_pass line here, depending if you use socket or port
}
}
This has worked for me for more than 5 years going.
location / {
try_files $uri/ $uri.html $uri.php$is_args$query_string;
}
Perhaps this may be of use for you... It' Simple and gets the job done:
location / {
rewrite ^/([^\.]+)$ /$1.html break;
}