Nginx not routing the request properly - regex

server {
listen 80 default_server;
server_name http://localhost;
server_tokens off;
root /usr/share/nginx/html;
location = /home {
index index.html index.htm;
try_files $uri /index.html;
}
location = /user {
index index.html index.htm;
try_files $uri /index.html;
}
location = /login {
index index.html index.htm;
try_files $uri /index.html;
}
location = /signup {
index index.html index.htm;
try_files $uri /index.html;
}
location ^~\/verify\/\?token=([A-Za-z0-9-_]*\.[A-Za-z0-9-_]*\.[A-Za-z0-9-_]*$) {
proxy_pass 'http://localhost:8000';
}
location ~ "^\/([0-9a-zA-Z+=-]{7,})$" {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass 'http://localhost:8000';
}
}
I have this nginx.conf file, I want to route /verify/?token=something.something.something(the value of token is jwt) to the backend.
But getting 404 error while trying to access the endpoint, all other routes are working properly.

Related

Nginx redirect after slash in location

I have domine name https://example.com/API/, I wanted to redirect anything given after /API/ for example :
https://example.com/API/test to https://example.com/API/
Below is my Nginx conf
location #error {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
root /var/www/html/test/;
index index.html index.htm;
internal;
}
location ~*/api {
rewrite ^/api(.*) $1 break;
proxy_pass http://127.0.0.1:3100;
client_max_body_size 60M;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
error_page 502 #error;
}
That from the above example if /API/ gets 502 I am redirecting it to. PHP file is working fine, But if there is anything given after /API/test it is showing 404 not found.
You can have something of this sort:
server {
root /var/www/html; #your own values
server_name _; #website name
location #error {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
root /var/www/html/test/;
index index.html index.htm;
internal;
}
location /api {
proxy_pass http://127.0.0.1:3100;
client_max_body_size 60M;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
error_page 502 #error;
}
}
So basically you would have to use location /api {} directive and that would work.

problem with serving static files of django on server with nginx

Hi everyone I have changed default file of nginx where is in /etc/nginx/sites-available/default as below
upstream django {
server 127.0.0.1:8000;
}
server {
listen 80;
location /static/ {
root /home/django/chistaa/chistaa/settings;
try_files $uri =404;
}
location / {
try_files $uri #send_to_django;
}
location #send_to_django {
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://django;
}
}
but it doesnt load my static files.
What did I wrong?!

How to run django and wordpress with nginx

I'm having problem with configuring my nginx.conf file to run django server on main domain and a WordPress site on domain.com/blog.
This is my configuration file which my WordPress dir is
/var/www/varzesh-kon/blog/:
upstream Main_Project_server {
server unix:/home/amirfarsad/django_env/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 2n9l.s.serverhost.name;
client_max_body_size 4G;
access_log /home/amirfarsad/logs/nginx-access.log;
error_log /home/amirfarsad/logs/nginx-error.log;
location /static/ {
alias /home/amirfarsad/Main_Project/static/;
}
location /media/ {
alias /home/amirfarsad/Main_Project/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://Main_Project_server;
break;
}
}
location /blog/ {
root /var/www/varzesh-kon/blog/;
index index.php index.html index.htm;
try_files $uri =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
root /var/www/varzesh-kon/blog/;
}
}
My django site works well but when I go to domain.com/blog, it gives me a 404 not found nginx page.
Try changing your bottom location block
location ^~ /blog {
root /var/www/varzesh-kon/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php$is_args$args;
}
Explanation:
In case of the root directive, full path is appended to the root including the location part
In your case:
location /blog/ {
root /var/www/varzesh-kon/blog/;
The final path that nginx will derive is going to be:
/var/www/varzesh-kon/blog/blog
That's why its showing you 404 not found page
Solution:
Either use alias instead of root
or change root path to /var/www/varzesh-kon/
location /blog/ {
root /var/www/varzesh-kon/;
...
}
location /blog/ {
alias /var/www/varzesh-kon/blog/;
...
}
for more: wiki

Nginx Base Auth

I work with Django and Nginx
I added the following entry to my config to restrict access to example.com/admin/
The function asks for a password, and everything works, but after that, as I get a 404 Not Found error from Nginx
Full config
upstream rates_core_server {
server unix:/webapps/example.com_app/example.com/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name example.com www.example.com;
client_max_body_size 4G;
access_log /webapps/example.com_app/logs/nginx-access.log;
error_log /webapps/example.com_app/logs/nginx-error.log;
location /admin/ {
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location /static/ {
alias /webapps/example.com_app/example.com/static/;
client_max_body_size 100M;
}
location /media/ {
alias /webapps/example.com_app/example.com/static/media/;
client_max_body_size 100M;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://example.com_server;
break;
}
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /webapps/example.com_app/example.com/static/;
}
}
I do not understand what the problem is
With the current config, nginx does not know where to look for or redirect to for admin block. Can you include proxy_pass settings in your admin block as well, like this:
location /admin/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://example.com_server;
break;
}
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}

Make the keystone supported website live on DigitalOcean

This is my server block nginx config, but I don't know what is the mistake. When I hit my domain name, it gives me the "404 Not Found".
Kindly help
server {
listen 80;
listen [::]:80;
root /var/www/squareeducation.in/SE_WebApp/template/views/layouts;
index index.html index.htm index.nginx-debian.html default.hbs;
server_name www.squareeducation.in;
location / {
try_files $uri $uri.hbs/ =404;
proxy_pass 'http://127.0.0.1:3000';
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_bypass $http_upgrade;
}
location ~ .*\.(img|gif|jpg|jpeg|png|bmp|swf|js|css)$ {
root /var/www/squareeducation.in/SE_WebApp/public;
try_files $uri $uri/ =404;
}
}
that means that your server cannot find where the files are located, also, I don't know your configuration, but if you are starting simple, you can try by just using:
listen 80;
listen [::]:80;
root /var/www/squareeducation.in;
index index.html index.htm index.nginx-debian.html default.hbs;
server_name www.squareeducation.in squareeducation.in;
location / {
try_files $uri $uri.hbs/ =404;
}
}
I personally always start from the simple stuff to troubleshoot. Put a basic index.html file on that route: /var/www/squareeducation.in
and see if it reads it, also apply the permissions: sudo chmod 775 /var/www/squareeducation.in
Here is a page with Nginx basic commands, it'll come handy:
https://elkepon.com/how-to-install-nginx-on-ubuntu-16-04/
Hope it helps