I need an Nginx configuration for the following requirements. I tried several methods but it's not working.
For Example: If I run the following URL in the browser, http://example.com/admin/test/update/image/list/100 Need to convert to http://example.com/admin/index.php?test/update/image/list/100. How to configure it in Nginx. if you have any idea about this please guide me. Thanks.
My Nginx configuration:
server {
server_name example.com;
root /home/example/public_html;
index index.php home.php login.php;
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml|rss|txt) {
try_files $uri $uri/ =404;
}
location / {
try_files $uri $uri/ =404;
}
location /admin/ {
rewrite ^ /admin/index.php?$1 break;
rewrite ^/([^\.]+)/?$ /admin/index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)/$ /admin/index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)$ /admin/index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)/([^\.]+)$ /admin/index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)/([^\.]+)/$ /admin/index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)/([^\.]+)/([^\.]+)$ /admin/index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)/([^\.]+)/([^\.]+)/$ /admin/index.php?$1 break;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php5.6-fpm-example.com.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php5.6-fpm-example.com.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Related
I have problem with friendly URLs. The script below is working but not currently like
/index.php works
/index works
/index/ doesn't work (I expect it to open the same page as /index)
how to fix this?
location / {
autoindex on;
root /var/www/plasti;
index index.php index.html index.htm;
if ($request_uri ~ ^/(.*)\.php$) { return 302 /$1; }
try_files $uri $uri/ $uri.html $uri.php?$args;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
#fastcgi_connect_timeout 300s;
fastcgi_read_timeout 120s;
#fastcgi_send_timeout 300s;
}
I'm trying to restrict direct access to .php files on web server.
Used allow localhost;deny all;. However, this restricts access also to index.php.
How to overcome this issue? Is there something like IF conditions?
My config:
if ($request_uri ~* "^(/)index\.php$") {
return 301 $1;
}
location / {
try_files $uri $uri/ /index.php?$args;
rewrite ^/(\w+)$ /?system=$1 break;
rewrite ^/(\w+)/(\w+)(/.)*$ /?system=$1&id=$2 break;
rewrite ^/(.*)/$ /$1 permanent;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/html/web/config.php";
}
}
What's commonly done is that you add a RewriteRule to an .htaccess file. This makes it possible to redirect all traffic to the index.php as you wish.
This solution even allows you to add the requested URL as an URL-parameter, so it is accessible within index.php as a $_GET variable.
Since .htaccess is only a solution for an Apache server it cannot be applied one-on-one here. This blog post on NGINX's website explains how it's done on NGINX: https://www.nginx.com/blog/creating-nginx-rewrite-rules/
Instead of matching all PHP files, match just index.php and reject all others, like this:
location = /index.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/html/web/config.php";
}
location ~ \.php$ {
return 301 $scheme://$http_host/index.php;
}
If you want to allow posts from the server to itself add the following for the URI
location = /post.php {
allow 127.0.0.1/24;
deny all;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/html/web/config.php";
}
Edit: Alternative Configuration
server {
listen 80;
location = /index.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/html/web/config.php";
}
location ~ \.php$ {
return 301 $scheme://$http_host/index.php;
}
}
server {
listen 127.0.0.1:81;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/html/web/config.php";
}
}
Local requests would then need to be directed to port 81, ie:
curl http://localhost:81/myscript.php
I am trying to get nginx to route all requests starting with /embed to /home/forge/dev.tline.io/embed/index.php
My Nginx config:
location /embed {
root /home/forge/dev.tline.io;
try_files /embed/index.php =404;
}
location / {
root /home/forge/dev.tline.io;
index index.html index.htm;
try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
All requests go to /embed/index.php but it doesn't run the php file it downloads it.
Note: http://dev.tline.io/embed/index.php is compiled not downloaded
I got it to work if add
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
into location /embed but there should be a better way to do this
This should execute /embed/index.php for all /embed URLs:
server {
root /home/forge/dev.tline.io;
location / {
index index.html index.htm;
try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
}
location /embed {
fastcgi_param SCRIPT_NAME $document_root/embed/index.php;
fastcgi_param SCRIPT_FILENAME $document_root/embed/index.php;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi_params;
}
}
Please try out the following code,
map $request_uri $rot {
"~ /embed" /home/forge/dev.tline.io/embed/;
default /home/forge/dev.tline.io/;
}
map $request_uri $ind {
"~ /embed" index.php;
default index.html;
}
server {
...
root $rot;
index index.php index.html index.htm;
...
location / {
try_files $uri$args $uri$args/ $uri $uri/ /$ind =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
...
}
If this doesn't work out, try switching $ind position suitably, and check error log in case of extra '/' found.
I'm trying to test DreamVids (https://github.com/DreamVids/DreamVids on branch v2) on my server with Nginx and PHP-FPM.
But i have a problem when i try to access it, it downloads a file.
My configuration is :
server {
server_name mydomain.fr;
root /home/dreamvids/DreamVids;
index index.html index.htm index.php;
error_page 404 index.php;
autoindex off;
location / {
try_files $uri /index.php$is_args$args;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?/$1 break;
}
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
Thanx for help
server {
listen 80;
root /home/dreamvids/DreamVids;
index index.html index.htm index.php;
server_name example.com;
location / {
try_files $uri /index.php$is_args$args;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?/$1 break;
}
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
I am trying to setup my Laravel 4 project using nginx . Here is my nginx server block for laravel :
server {
listen 80;
root /home/prism/www/laravel/public;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
But my problem is , Its showing "404 not found" error for all other routes except the default one , that comes with default installation .
This is an NGINX Configuration i've used with Laravel 4 and Laravel 4.1 that works.
server {
listen 80;
server_name sub.domain.com;
set $root_path '/var/www/html/application_name/public';
root $root_path;
index index.php index.html index.htm;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}
location ~ /\.ht {
deny all;
}
}
you could try this for location / { ... }
location / {
try_files $uri $uri/ /index.php?$query_string;
}
$query_string worked for me.