Nginx disable url rewrite for specific path / url - mod-rewrite

I have the following nginx configuration for the url rewrite
location / { ##merge
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location /devtools/phpmyadmin/ { ##merge
root /var/www/domain.tld/web;
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/lib/php5-fpm/web2.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
}
in the /var/www/domain.tld/web/ directory have an /api/ directory and I want disable for the url rewrite for this. So if I woud like access from url with index.php: http://domain.tld/api/index.php/function/method
How can I modify the nginx config file?
Edit:
I try the following rewrite, but not working:
location = /api {
rewrite ^ /api/index.php;
}

I confess that I do not understand your configuration file. Generally, you would define a root to be inherited by all (or most) of your location blocks. And usually a separate location ~ \.php$ block to handle off-loading .php scripts to a PHP processor via FastCGI.
Ignoring your location /devtools/phpmyadmin/ block for the moment, a typical implementation would look like this:
root /var/www/domain.tld/web;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/lib/php5-fpm/web2.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
The nginx directives are documented here.
If you want URIs that begin with /api to use a different controller, you could add:
location /api {
try_files $uri $uri/ /api/index.php;
}
I cannot see the purpose of the location /devtools/phpmyadmin/ block, as it does not seem to add any more functionality.

Related

Nginx and Laravel: How to restrict "location" block to from "/" to "/api"?

I have a Laravel application that I would like to use only for its /api routes. When /, /login, /profile, etc are accessed, I would like nginx to serve the index file located in the base root, /var/www/html/index.html.
Here's my current default.conf. The /api routes work, but it also serves / from the backend, which I don't want.
If I simply change location / to location /api, then the /api routes become inaccessible, and trying to access them returns the index file located in the base root, /var/www/html/index.html. This is the opposite of what I am trying to achieve. Haha.
How can I keep the /api routes accessible, while also preventing / from being served by the backend?
server {
listen 80;
root /var/www/html;
server_name _;
index index.php index.html index.htm;
# API routes should be handled by the backend (Laravel).
##### I want to change the following line from "/" to "/api".
location / {
root /var/www/backend/public;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
}
}
According to your configuration, that should already be happening.
the directive:
try_files $uri $uri/ /index.php?$query_string;
Will attempt first to locate the static file, and if the static file exists and is not a php file, it will be served by nginx directly. Only if it is a php file will it be served with the php backend.
If you want to prevent .php files from being executed at all, you can modify to separate the / location from the /api location:
location ~ ^/api/.*\.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
I accomplished this, but somewhat inelegantly.
I have 2 location blocks:
location ~ "^(?!/api).*$" matches all routes except those that begin with /api.
location / matches all other routes. The reason that it does not match all routes is simply that nginx matches location blocks with regular expressions first.
Strangely, using a regular expression for the second block to match all routes that do begin with /api did not work. I still don't understand why.
server {
listen 80;
root /var/www/html;
server_name _;
index index.php index.html index.htm;
# All non-API routes should be handled by the frontend.
# Use a regular expression to identify all requests
# that do NOT begin with "/api".
location ~ "^(?!/api).*$" {
root /var/www/frontend/public;
try_files $uri $uri/ /index.html =404;
}
# API routes should be handled by the backend (Laravel).
# (Since regex-based location blocks are matched first,
# this will be a fallback to the above location block).
location / {
root /var/www/backend/public;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
}
}

Nginx server laravel route directory 404 error on Windows

I'm getting a 404 every time I try to open a page other than the welcome page. I've read all the posts here on how to fix the problem, but none of the solutions worked for me.
I'm on Windows 10 and use WPN-XM for the Nginx server.
My laravel project is installed in C:/server/www/myapp
My configuration looks as follows:
server {
listen 127.0.0.1:80;
server_name localhost;
root www;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php_pool;
fastcgi_index index.php;
#fastcgi_param PHP_FCGI_MAX_REQUESTS 1000;
#fastcgi_param PHP_FCGI_CHILDREN 100;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param REMOTE_ADDR $http_x_real_ip;
# mitigate namespace conflict, see httpoxy.org
fastcgi_param HTTP_PROXY "";
include fastcgi_params;
}
I tried replacing root www with "C:/server/www/myapp/public" and a couple of other options.
I used try_files $uri $uri/ /index.php$is_args$args instead of try_files $uri $uri/ /index.php?$query_string
Any help will be appreciated.

nginx one domain and multiple projects setting alias

I want to use one domain to map different project.
mydomain.com.tw => /var/www/project1/public
mydomain.com.tw/test => /var/www/project2
but mydomain.com.tw/project2/detail always direct to mydomain.com.tw project route.
project1 => laravel
project2 => vue and using vue-route
listen 80;
listen [::]:80;
server_name mydomain.dev;
index index.php index.html index.htm;
root /var/www/project1/public;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /lottery {
alias /var/www/project2/dist;
try_files $uri $uri/ #rewrites;
}
location #rewrites {
rewrite ^(.+)$ /index.html last;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
I solved this question.
Solution:
https://gist.github.com/Maras0830/dc6f627eba005bdfc6b741f7f2ea3178

NGINX Mod_rewrite, Subfolder redirect to folder

I'm novice and i have a problem with the rewrite mod in NGINX.
I give you the environement:
I have 2 app:
each one www/
another one www/atri/
both app need to be a Mod_rewrite, so i use this :
location / {
try_files $uri $uri/ /index.php?$args;
}
This work completely for the first app, BUT, when I go on the second app /atri/ this load all the app like the same root of the first app in the folder /www/.
Do you have an idea to separate the 2 app in the conf?
For information I use codeigniter.
Thanks for your reply.
Try:
location / {
try_files $uri $uri/ /index.php?$args;
}
location /atri {
try_files $uri $uri/ /atri/index.php?$args;
}
See this document for more.
location /atri/ {
alias /var/www/html/atri/;
try_files $uri $uri/ /atri/index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
try this!

Laravel nginx.conf with official Heroku php buildpack?

I have the following in `nginx.conf in my project root
location / {
try_files $uri $uri/ /index.php?$query_string;
}
But only in the / path works, all others are coming up with a 404 error.
How can I make Laravel work on heroku with nginx?
This ended up working for me:
Procfile:
web: vendor/bin/heroku-php-nginx -C nginx.conf public/
nginx.conf
location / {
# try to serve file directly, fallback to rewrite
try_files $uri #rewriteapp;
}
location #rewriteapp {
# rewrite all to app.php
rewrite ^(.*)$ /index.php$1 last;
}
You are missing fastcgi_* directives; try this after your location directive:
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;
}
Source: How to Install Laravel with an Nginx Web Server on Ubuntu 14.04

Resources