I have a docker running Nginx on port 80 and I port forwarded to 8080, so I can see it locally in localhost:8080
Using ngrok I created a url. Using the newly created url I can see the Welcome Page of Nginx locally. Lets say my url is http://myurl.ngrok.io/
All I want now is Nginx to listen to requests and if I ask http://myurl.ngrok.io/mylocation it will return 200 and print YES!!!.
This is how I configured nginx.conf:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 8080 default_server;
server_name _;
add_header X-debug-message "A static file was served" always;
return 200;
location \mylocation {
return 200 'YES!!!';
}
}
}
After I configured nginx.conf file I hit:
root@ec24***f108:/etc/nginx# nginx -s reload
2021/02/21 13:47:55 [notice] 1048#1048: signal process started
Then going to http://myurl.ngrok.io/mylocation I get 404!
What am I doing wrong?
You have a
return 200;
in theserver
block, which will be processed before thelocation
statements. Only putreturn
statements in theserver
block if that is the only thing theserver
block needs to do, otherwise, it need to be inside another block.The
location
statement literally matches the URI/*
. If you want alocation
that matches any URI, uselocation /
. See this document for details.This line means that *.conf (any .conf file) will get higher priority than nginx.conf (the file I showed in my question).
Inside /etc/nginx/conf.d/default.conf there is a server { } section that gets loaded anytime I asked for the site, and the server { } I wrote in nginx.conf was not considered.
All I had to do is to mark out the include line from nginx.conf, and then reload the Nginx with
Now every time the page is loaded, the nginx.conf is considered.