Nginx routing issue - nginx

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.

Related

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/

Difference between 2 methods rewrite in 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.

Custom 404 page not displaying

I just started with Nginx, and did the basic configuration, regarding configuring the PHP FPM, and I've changed the index file to be index.php instead of index.html. I also managed to get a handle on URL rewriting, although omitted from my example below for simplicity.
But in the default configuration file, there is a section dedicated to error pages, which looks as if it's ready to "plug and play". I have uncommented them, but they don't work correctly. I've gone through some 404-related questions on Stackoverflow, everyone seems to recommend that as the correct syntax.
Maybe the order of instructions is wrong, however, this is from the original sample configuration, so I don't know why it wouldn't be working.
I have remove all the commented lines from the configuration file, what's left is this:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/html;
index index.php;
server_name localhost;
location / {
try_files $uri $uri/ index.php;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Now, when I go to http://localhost/this-page-doesnt-exist, the browser displays a literal File not found., instead of the contents of my /404.html file.
Also, the permissions of my 404.html file is the same as the permissions for the index.php, which is 644. The owner is the same as well.
What could be wrong?
try_files is passing your request for a none-existent file to index.php. This is in turn sends the request to php-fpm. By default Nginx returns the response from php-fpm to the client which is what you are seeing.
Update your php-fpm config to look like this and inform Nginx to handle error codes in the response.
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_intercept_errors on;
include fastcgi_params;
}
The documentation is here: fastcgi_intercept_errors

nginx redirects every http request to https

finally i switched to the nginx webserver. But everytime i access for example http://mywebsite.com it redirects me to https://mywebsite.com. I dont have any ssl options in my server block (vhost). Here a stripped down version (only removed help comments):
server {
listen 80;
root /usr/share/nginx/www/mywebsite/htdocs;
index index.php index.html index.htm;
server_name mywebsite.com;
location / {
try_files $uri $uri/ /index.html;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param CONTEXT Staging;
include fastcgi_params;
}
}
I don't really know if i am on the correct place to search for the bug?!
PS: PHP returns me ["SERVER_PROTOCOL"]=> string(8) "HTTP/1.1"
Thanks in advice!
Got it! I've found in my /etc/nginx/fastcgi_params that line fastcgi_param HTTPS $https; which i commented out. Now, it works fine. Hope that helps someone else.

Short-circuit logic in Nginx conf (would like to override a location)

I have mediawiki installed. All is right in the world except for when I try to alias a external directory (webalizer web stats). I see that Nginx passes off the request to /usage/* to PHP/Mediawiki. I don't want that. I literally want everything under /usage/ to point to my alias and nothing else. Completely separate from Mediawiki code and functionality.
# in no way related to Mediawiki. I just want to serve this as static HTML.
location /usage {
alias /var/www/webalizer/wiki.longnow.org/;
}
# This answers to anything, which may be my problem
location / {
try_files $uri $uri/ #rewrite;
index index.php;
}
# A special rewrite to play nicely with Mediawiki
location #rewrite {
rewrite ^/(.*)$ /index.php?title=$1&$args;
}
# PHP, nom nom nom
location ~ \.php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/tmp/php-fastcgi.socket;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
I was hoping that listing the /usage location directive ahead of the rest would short-circuit the system, but I have been spoiled by Django ;)
To stop Nginx from processing further location directives, it should be prefixed by ^~.
I think you will still want a try_files falling back to a 404 response inside the location.
location ^~ /usage {
alias /var/www/webalizer/wiki.longnow.org/;
try_files $uri $uri/ =404;
}
See http://wiki.nginx.org/HttpCoreModule#location for reference.

Resources