Nginx rewrite path to query string - php

I use nginx (php-fpm) and fatfree framework. I need some redirect logic in my routing engine. It looks like:
http://example.net/page/...
Instead of dots there could be something like:
another.net/someurl
http://another.net/?local_query
another.net/path/to/article.html
It breaks nginx logic and I see 404 error.
My nginx config looks simply:
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include /etc/nginx/conf.d/fastcgi_params.conf;
fastcgi_param SCRIPT_FILENAME /var/www/$main_host/www$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT /var/www/$main_host/www;
fastcgi_param PHP_ADMIN_VALUE upload_tmp_dir=/var/www/$main_host/tmp/upload;
fastcgi_param PHP_ADMIN_VALUE session.save_path=/var/www/$main_host/tmp/sessions;
}
So when I use:
http://example.net/page/another.net/path/to/article.html
Nginx tries to find file in filesystem with name article.html as I understand. How to write rule to ignore any symbols when there is keyword page after domain?

Solution #1:
Probably it doesn't work because there is no fastcgi_pass directive inside /etc/nginx/conf.d/fastcgi_params.conf.
If so, all you need is simply add it to your \.php$ location. In general case it looks like:
fastcgi_pass 127.0.0.1:9000;
but this line might be different, depending on how PHP-FPM is configured.
Solution #2:
Your current configuration makes Nginx pass request to PHP-FPM only if URI ends with .php.
To make Nginx do it when URI starts with /page/ as well, you should write a new location with almost the same content (except for SCRIPT_FILENAME line) and (important!) put it before existing \.php$ location.
Copying location content would cause a code duplication, and I would recommend write the config in a slightly different way:
fastcgi_param SCRIPT_FILENAME /var/www/$main_host/www$my_script_name;
fastcgi_param DOCUMENT_ROOT /var/www/$main_host/www;
fastcgi_param PHP_ADMIN_VALUE upload_tmp_dir=/var/www/$main_host/tmp/upload;
fastcgi_param PHP_ADMIN_VALUE session.save_path=/var/www/$main_host/tmp/sessions;
include /etc/nginx/conf.d/fastcgi_params.conf;
location ~ /page/ {
fastcgi_pass 127.0.0.1:9000;
set $my_script_name /path/to/file.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
set $my_script_name $fastcgi_script_name;
}
In this case you will have 2 scenarios:
if URI starts with /page/, Nginx will run /var/www/$main_host/www/path/to/file.php.
if URI ends with .php, Nginx will run requested php file, as usual.
Solution #3:
Leave your \.php$ location as is and put the following location before it:
location ~ ^/page/ {
include /etc/nginx/conf.d/fastcgi_params.conf;
fastcgi_param SCRIPT_FILENAME /var/www/$main_host/www/path/to/file.php;
fastcgi_param DOCUMENT_ROOT /var/www/$main_host/www;
fastcgi_param PHP_ADMIN_VALUE upload_tmp_dir=/var/www/$main_host/tmp/upload;
fastcgi_param PHP_ADMIN_VALUE session.save_path=/var/www/$main_host/tmp/sessions;
}
Just replace /path/to/file.php with real path to file which should handle such requests.

Related

Is it possible to have web index outside public html?

Say a website has this folder structure
/index.php
/<public>
dikpic.jpeg
And when someone visits the website I want the physical web root to point to /public,
like mywebsite.com/dikpic.jpeg
(without url rewrites).
This can be achieved with the root /myuser/public; command.
But I also want to load the index.php file from outside this directory:
index /myuser/index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~* \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm-myuser.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
The problem is with the location try files block that assumes / is the web root :(
Any way around this?
I should also point out that the index.php is the only script on the website.
So I don't really care if any other requests for .php files are ignored. The index.php handles url rewriting stuff...
You can use an additional location block for that, although it doesn't seems an elegant solution to me:
location ~* \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm-myuser.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
location = /index.php {
fastcgi_pass unix:/run/php/php7.4-fpm-myuser.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /full/path/to/your/index.php;
fastcgi_param SCRIPT_NAME /index.php;
}
Exact matching locations have a priority over regex matching ones, so the first block will be used for any PHP file request except the /index.php. You don't even need to define a root for the second location, setting right SCRIPT_FILENAME FastCGI parameter value will be enough.
Update
I didn't notice the very last sentence of your question, but if you didn't care for any other PHP files, only the second block location = /index.php { ... } will be enough.
You can create symlinks of your index.php here is how to do that.
And as result you will have single index.php to all your websites

How to pre-interpret php in .html files on NGINX?

I have found a few threads on this, but the solutions I find do not work. I am trying to get my NGINX server to interpret the php inside of html files before sending the html files to the browser for processing. Here is some code inside my server bracket - inside of my only sites-enabled directory file, "default:"
location ~ \.(php|html|htm)$ {
include snippets/fastcgi-php.conf;
root html;
fastcgi_index index.php;
# With php-fpm (or other unix sockets):
fastcgi_param HTTP_PROXY "";
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
include fastcgi_params;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}
Specifically, the part with (php|hmtl|htm) I was hoping to be the solution. However, php in my .html files are still commented out and not processed.
Not sure where security.limit_extensions parameter is? Here is my fastcgi-php.conf in snippets folder:
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi.conf;
Thanks for any ideas.
I don't know what is inside your snippets/fastcgi-php.conf and if it is additionally limiting the PHP code processing via PHP-FPM somehow (I personally prefer to write PHP handlers locations myself), but at least you'll need to adjust the security.limit_extensions parameter in your PHP-FPM pool configuration file (defaults to .php .phar). If it does not help, append the snippets/fastcgi-php.conf file contents to your question.

CodeIgniter 3, Nginx rewrite how to?

I installed CodeIgniter 3 after a long time on PHP-fpm and nginx (Ubuntu). Previously I had always used CodeIgniter on Windows and configuring it on Windows and Apache it was a piece of cake.
Now I wanna install it on nginx, because I wanna use nginx-push-stream-module, which isn't possible from apache.
Now when I'm configuring it, its not working.
If I type localhost/myexample.com or localhost/myexample.com/index.php it works (myexample.com is the name of that directory)
but when I try to access
localhost/myexample.com/welcome
or
localhost/myexample.com/welcome/index
or
localhost/myexample.com/index.php/welcome
or
localhost/myexample.com/index.php/welcome/index
it doesn't work in any of the 4 cases (with or without index.php)
My root directory is /var/www/html/myexample.com
I tried all of the rewrite settings available online (including the following settings) from different blog posts etc (as I'm not used to nginx myself)
server {
server_name myexample.com;
root /var/www/html/myexample.com/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
expires 15d;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/myexample.com/index.php;
fastcgi_param REQUEST_URI $request_uri;
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_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
Edit: I also tried the method mentioned at Nginx's official website, but that's also not working.
You should modify your /etc/hosts and add this line:
127.0.0.1 myexample.com
And after that use myexample.com or myexample.com/welcome to access your CodeIgniter site
First - confirm what the server name is going to be. Set that in the your hosts file, then confirm the web root.
So in /etc/hosts add
127.0.0.1 myexample.com
Then your index.php file should be in
/var/www/html/myexample.com/
You should get CI up and working on the url
http://myexample.com

Increase fastcgi_read_timeout for single route

I have the following Nginx configuration for forwarding requests to a PHP-FPM backend:
server {
...
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~* \.php$ {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
One specific route in the app needs a slightly longer php max_execution_time setting. I've configured this successfully and verified it works by setting a longer fastcgi_read_timeout in the above config.
However, I don't need this to be applied to every single route. I'm guessing I need a nested location somewhere but nothing I've tried seems to work!
The fastcgi_read_timeout directive does not appear to accept dynamic values, so a separate location block for the special route will be required. Looking at your configuration file, I assume the special route is a unique URI processed by the /index.php script. Something like this should work:
location ^~ /special/route/uri {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_read_timeout 100s;
}
You can use a prefix location with the ^~ modifier (as above) to override the regex location that usually processes PHP files. Alternatively, you can use a regex location, but place it above the existing regex location so that it takes precedence.
See this document for location syntax.

Nginx Rewrite Rule for all .PHP files/requests to be parsed by index.php

Is it any magic rewrite (like it does on Apache) for nginx to be able to rewrite URLs like '/submit.php' to be able to process them from index.php? We have lots of 404 not found errors because of the site structure and all previous URLs were like '/addrate.php', '/my_settings.php', '/profile.php' --> there are over 50 files like this and it would be very unprofessional and code-unwise to create a separate .php file for each of these functions, instead of parsing them all through index.php and using the needed classes like we do with the other rewrites.
Can you please find a solution/give us a suggestion today about this?
I think some info about this is here, but I want the exact reversed result:
http://www.nullis.net/weblog/2011/05/nginx-rewrite-remove-file-extension/
This configuration allow you to handle all URL (non-existing files in file system) with one php script placed in /var/www/example.org/htdocs/index.php
server {
listen 80; ## listen for ipv4
server_name example.org www.example.org;
root /var/www/example.org/htdocs;
access_log /var/log/nginx/example.org.access.log;
error_log /var/log/nginx/example.org.error.log;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ #php;
}
# enable running php files under php fastcgi
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.org/htdocs$fastcgi_script_name;
#fastcgi_param QUERY_STRING $uri;
include fastcgi_params1;
}
location #php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.org/htdocs/index.php; # this script catches all non existing URLs
fastcgi_param QUERY_STRING $uri;
include fastcgi_params1;
}
}

Categories