Difference between 2 methods rewrite in nginx - nginx

I'm migrating from Apache to nginx and need to convert massive httaccess file to nginx format.
I found 2 ways that work, which one should I use?
location = /test.html { rewrite ^(.*)$ /index.php?action=temp&name=test; }
or just
rewrite ^/test.html$ /index.php?action=temp&name=test;
I'm putting this all in the file (ez_rewrite_list.conf) and then include in the virtual.conf. Where should I put that file location wise? Does it matter? am I doing right? any tips
server {
listen 80;
server_name test.com;
location / {
root /var/www/com/mysite;
index index.php index.html index.htm;
}
include /etc/nginx/ez_conf/ez_rewrite_list.conf;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /var/www/com/mysite;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME document_root$fastcgi_script_name;
include fastcgi_params;
}
}

You've forgot to escape dot (.) in rewrite and they are not exactly the same.
Technically exact location should be a little faster than checking regexp for every request. Also you don't need to capture anything in rewrite inside the location, so I would use
location = /test.html {
rewrite ^ /index.php?action=temp&name=test;
}
But actually, you'll never see any difference.

Related

Nginx routing issue

Not entirely sure how to ask this but I have also looked all over the web and couldn't find an answer so any help is much appreciated.
I'm trying to set up an API call through my site that uses nginx, if I send the url /api/timestamp/ it works just fine and returns what is intended. However if i add a parameter and send /api/timestamp/2015-08-09 it tries to open the file 2015-08-09 which obviously doesn't exist.
How do I get Nginx to pass the parameter as an argument to my program and not try to use it as a route? Or am I looking at this all wrong?
server {
listen 83 default_server;
server_name portfolio.com;
access_log /var/log/nginx/port.access.log;
error_log /var/log/nginx/port.error.log;
root /var/www/portfolio/;
index index.php;
error_page 404 /404.html;
error_page 405 =200 $uri;
location /api/timestamp/ {
rewrite /api/timestamp/(.*) /api/timestamp/?param=$1 last;
}
location ~ \.php$ {
try_files $uri $uri/ =404;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi.conf;
}
}
you need use a rewrite rule, the most simple solution for you is some like this
location /api/timestamp/ {
rewrite /api/timestamp/(.*) /api/timestamp/?param=$1 last;
}
Then you will receive the value in a parameter named param (change it according your requeriments).
Obviously, you can use a regex most restrictive to avoid undesired values .
You can get more information in https://www.nginx.com/blog/creating-nginx-rewrite-rules/.
Regards.

Nginx rewrite to prevent PHP file direct access

I'm trying to setup some rewrites so that
(1) .com/images/* will load naturally
(2) .com/* will be rewritten to .com/loader.php?control=*
However the code below works perfectly except it will execute .com/config.php instead of rewriting it to .com/loader.php?control=config.php
How can I prevent my rewrite being overridden? I only want .php files to be executed if it's loader.php or in the images folder. (Been trying for hours)
server {
listen 80;
root /mnt/web/test_com/public_html;
server_name test.com;
index index.html index.php index.htm;
location ~ .php(.*)$ {
fastcgi_pass unix:/tmp/php-70-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $DOCUMENT_ROOT$fastcgi_script_name;
fastcgi_param PATH_INFO $2;
include fcgi.conf;
}
location / {
rewrite ^/(.*)$ /loader.php?control=$1 last;
}
location /images/ {
}
}
Thanks!
You should look into the try_files directive
Like the return and rewrite directives, the try_files directive is placed in a server or location block. As parameters, it takes a list of one or more files and directories and a final URI
Read the whole blog-post written on the nginx blog here:
https://www.nginx.com/blog/creating-nginx-rewrite-rules/

Nginx load subpath as wordpress root

I'm trying to set up a Wordpress in a system that has another php application installed, using nginx as web server.
I've simplified my config file to the maximun. The following confi is serving one post of my blog:
server {
listen 80;
server_name blog.ct.com;
root /home/ff/www/blog/;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$uri&$args =405;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_buffer_size 128k;
fastcgi_buffers 64 32k;
fastcgi_busy_buffers_size 128k;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param APPLICATION_ENV development;
fastcgi_param HTTP_X_FORWARDED_PROTO https;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
But, due my system's requirements I need to serve the blog from within a sub path (In my final system http://blog.ct.com/ should be serving my custom php app and http://blog.ct.com/vendor should be serving the wordpress blog).
The local root directory from wordpress must be /home/ff/www/blog/ (this cannot be changed, while my custom app's directory is /home/ff/www/myapp/). So I think I need to reserve location / for my custom app, I have to create a location /vendor
If I add /vendor and I return 403 in / (just to debug easier), the browser says 405 (notice the =405 in /vendor, also to debug easier):
location /vendor {
try_files $uri $uri/ /index.php?$uri&$args =405;
}
location / {
return 403;
}
So I think nginx is going into location /vendor but is not finding my php script in /home/ff/www/blog/index.php so its returning the fallback 405.
Any idea why this could happen?
How can I achieve to load http://blog.ct.com/vendor as the root from wordpress but keeping http://blog.ct.com/ using another php script?
I've found out the following hints that gave me the clue to fix the problem (in case someone has the same problem than me, this may help)
Using location /path is not the same as using location ~(/path) (regex have different priority, so maybe they are not being checked in the order you think)
Adding error_log /your/path/log/error.log debug; to any location block may help you to see how is nginx serving every request (e.g. to location fastcgi, location \vendor, or the server{ block).
alias /var/www/path/vendor works different than root /var/www/path/vendor (check Nginx -- static file serving confusion with root & alias);
In case of the root directive, full path is appended to the root including the location part, whereas in case of the alias directive, only the portion of the path NOT including the location part is appended to the alias.
using rewrite with alias can help you parse the php file you want independent of the path
if (!-f $request_filename) {
rewrite ^ $document_root/index-wp.php last;
}
Take care of the SCRIPT_FILENAME you are using (check it with error_log, see above), maybe you need fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; but you are loading fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; so depending on your previous config you may be attaching the document root twice.
Two different configurations for fastcgi can be used if you change your index.php file names. E.g. location ~ wp\.php$ { will work with wp.php while location ~ \.php$ { will work with all other php files like index.php.

How can I combine rewrite and client_max_body_size in nginx?

I want to enable large file uploads to a single location, and that location is then rewritten to another location. It seems that the rewrite is resetting the other configurations.
My Config:
server {
listen 80;
server_name example.com;
root /var/www;
index index.php;
# this location requires large file upload
location /upload {
client_max_body_size 512M;
rewrite ^(.*)$ /index.php?request=$1 last;
}
# all other locations
location / {
rewrite ^(.*)$ /index.php?request=$1 last;
}
# pass the PHP scripts to FPM
location ~ \.php$ {
include /etc/nginx/includes/php;
}
}
If I move the client_max_body_size out of the location and into the server, then it works.
If I put it in both the location /upload AND location ~ \.php$, then it also works. But I don't want other locations to be able to upload large files.
I was thinking I could direct to PHP directly in the location /upload, but once I run the rewrite it will look for another location anyway. Does that mean I would have to have two separate locations for php scripts? Is there any way to make the client_max_body_size retain through other locations after the rewrite?
If you need a specific client_max_body_size it needs to be set within (or inherited by) the location that processes the final URI. In your case, that is location ~ \.php$.
As you indicate in your question, the simplest solution is to process the PHP file in location /upload. This is easy to achieve, as you already have the PHP configuration in a separate include file.
Either a rewrite ... break; or overriding two of the fastcgi_param directives should work for you:
Option 1:
location /upload {
client_max_body_size 512M;
rewrite ^(.*)$ /index.php?request=$1 break;
include /etc/nginx/includes/php;
}
See this document for details.
Option 2:
location /upload {
client_max_body_size 512M;
include /etc/nginx/includes/php;
fastcgi_param QUERY_STRING request=$uri&$query_string;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}

nginx + php-fpm Rewrite rule not working

I have this conf,
rewrite "^download/([0-9a-f]{32})/(.+)$" /download.php?h=$1&f=$2 last;
location / {
index index.html;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.apache.sock;
fastcgi_index index.html;
fastcgi_param SCRIPT_FILENAME $root_path$request_filename;
include fastcgi_params;
}
But when I try open url, http://example.com/download/d3ef6bbeaff9b429680bca646e8ee1cf/video.mp4
It's return 404 Not Found
I tried put rule in any location, but it's not helped.
Direct link to file http://example.com/download.php is working, what need to do for work rewrite ?
I saw many threads about it, but solutions not helped me, whats wrong?
On server nginx + php-fpm
Nginx's rewrite always match full URI that starts with slash. So you need to fix your rewrite:
rewrite "^/download/([0-9a-f]{32})/(.+)$" /download.php?h=$1&f=$2 last;

Resources