Why does this rewrite in NGINX not work? - php

Trying to add a rewrite in local sever but I'm stuck.
This is what I have.
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
try_files $uri $uri/ $uri.html $uri.php?$query_string;
}
location /news/post/ {
rewrite ^/news/post/([a-zA-Z0-9-])?$ /news/post.php?id=$1 break;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
#extra
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#--extra
}
When going to /news/post/ it says "You have chosen to open this file" and ask if I want to download a BIN file.
If I go to /news/post/this-is-a-news-title I get a 404 Not Found.
What am I doing wrong?

Works. Changed a bit and needed the full path.
location /news/post/ {
rewrite ^/news/post/(.*)$ http://www.myserver.com/news/post.php?id=$1 break;
}

Related

Configuring php with nginx inside a reactjs app

I have a nginx configuration for a react app. I however would also like to include a sitemap.php that I build dynamically with php.
So here is my nginx config:
server {
listen 80;
listen [::]:80;
server_name mysite.com;
index index.html index.htm;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
location /sitemap.xml {
alias /var/www/web-app/public/sitemap.php;
}
location / {
root /var/www/web-app/public;
try_files $uri $uri/ /index.html;
default_type "text/html";
}
}
The snippets file consist of this:
# 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;
Also, this is hosted on an Ubuntu 16.04 digitalocean VPS.
My react app still loads fine. It is based on the index.html in my site root (/var/www/web-app/public). If I put test.php in the public folder, I get a 404 error. For my sitemap.xml alias, it forwards correctly to sitemap.php (also in public) but the php does not render.
So my two biggest issues here:
1. Why am I getting a 404 on /mysite.com/test.php?
2. And why is my php not rendering when it does work? (i.e. sitemap.php)
You are missing a root statement for your location ~ \.php$ block, so your PHP files will not be found. As this seems to be a common root with the location / block, simply move the statement up to server block scope:
root /var/www/web-app/public;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
location / {
try_files $uri $uri/ /index.html;
default_type "text/html";
}
There are a number of ways to redirect /sitemap.xml to /sitemap.php, but a rewrite...last will be simplest and invisible to users:
location = /sitemap.xml {
rewrite ^ /sitemap.php last;
}
See this document for location syntax, and this one for the rewrite directive.

filesmatch htaccess convert to nginx (on plesk)

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.

CodeIgniter calls /index.php instead of /basefolder/index.php , on Nginx?

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.

Nginx configuration for Laravel 5.2 and PHP 7: Routes not working

I'm trying to set up an AWS Ubuntu server to host a Laravel project. I'm having issues getting the routes working, and I suspect my Nginx virtual host config file is the culprit.
I think specifically the try_files line is the issue? I've tried try_files $uri $uri/ /index.php$is_args$args; as well as try_files $uri $uri/ /index.php?$query_string;, as well as a few others, with several wrong results, including:
404 errors
500 errors
Being linked to a download of the source code
And it's current behavior, being redirected to /index.php/ (which shows the Laravel welcome page).
Here's my /etc/nginx/sites-enabled/default:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/html/public;
index index.php index.html index.htm;
# Make site accessible from amazon aws
server_name xxxx.us-west-2.compute.amazonaws.com;
location / {
# Try file, then try folder, then pass to Laravel's /public/index.php
try_files $uri $uri/ /index.php$is_args$args;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
This is the current status of the file, and like I said, it currently redirects any real routes to blahblah.com/index.php/ and displays the Laravel welcome page. Fake routes that haven't been set up result in the Laravel 404 page.
I'm stumped because there's several guides to this and I've tried all the suggestions I can find, so I must be missing something.
It would appear php7 is working correctly and isn't the issue, or could this be a problem in a php config file?
DEVELOPMENTS:
It seems to work correctly if I access the site using its IP (Thanks Oliver Queen), any idea how I can get this to work when using the domain name?
Replace your current location / with the following code:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
And then replace the location below it with:
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

nginx redirection to sibling folder and serve php not working

I have my nginx home at /opt/nginx, inside there are site1 and mail folders, site1 has html folder that is a wordpress installation and mail is a webmail site, both of them must be proxied to php-fpm, site1/html works like a charm no problem at all.
I have the domain1.com and my server delivers site1/html content when domain1.com is requested.
What I want to do is, when domain1.com/mail is requested, serve the content of mail folder (sibling of site1). If I left a index.html file inside mail, when domail1.com/mail is requested, index.html is served to the client without problem but if I try to deliver mail/index.php, 404 error rise instead, what am I doing wrong? below my config:
/etc/nginx/conf.d/domain1.com.conf
server {
.
.
.
root /opt/nginx/site1/html;
index index.html index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /mail {
root /opt/nginx/;
try_files $uri $uri/mail mail/index.php;
}
location ~ [^/]\.php(/|$) {
# SECURITY : Zero day Exploit Protection
try_files $uri =404;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
You can use nested location blocks to invoke PHP scripts which are in a different document root.
Like this:
root /opt/nginx/site1/html;
index index.html index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ^~ /mail {
root /opt/nginx;
try_files $uri $uri/ /mail/index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm.sock;
include fastcgi_params;
}
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm.sock;
include fastcgi_params;
}
Notice the ^~ modifier which allows the nested PHP block to take precedence over the outer PHP block. I also removed the path info code which was not being used.

Categories