nginx config still serving default home page after root has been set - nginx

map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server{
server_name sample.com;
listen 80;
location = / {
root /root_path;
index index.html;
}
location / {
# root /root_path;
# index index.html;
# proxy_pass http://127.0.0.1:5200;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header Host $host; # pass the host header - http://wiki.nginx.org/HttpProxyModule#proxy_pass
# proxy_http_version 1.1; # recommended with keepalive connections - http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_version
# WebSocket proxying - from http://nginx.org/en/docs/http/websocket.html
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection $connection_upgrade;
}
}
This is only config file in "/etc/nginx/site-enable", but still its load nginx default page. How is this possible? If default root is not defined within server block but only within location blocks does root defaults to nginx default root directory?

I found issue and solution for my question.
"It should be noted that using an index file causes an internal redirect, and the request can be processed in a different location.
For example, with the following configuration:
location = / {
index index.html;
}
location / {
...
}
a “/” request will actually be processed in the second location as
“/index.html”. "
I Quote this from
> http://nginx.org/en/docs/http/ngx_http_index_module.html
In my case,i'm requesting to load index.html in first location block.
location = / { }
but nginx redirect and catch it from second location block.
location / { }
but index.html file is commented there. so it'll load the default nginx page.

Related

Changing the static root based on the host in Nginx

Is it possible to change the /static/ folder based on the host?
What I want is this:
server {
server_name code-server.domain.com;
location / {
proxy_pass http://localhost:8080/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
}
if ($host = code-server.domain.com/proxy/8000) {
location /static/ {
root /home/...
}
}
Is this possible?
I am using code-server, they have a proxy built in. But would I would like to see is when visiting code-server.domain.com/proxy/8000, a different static root loads. (for a development server)
Any help is appreciated!

Problem with nginx location and proxy_pass

I have a rule in my nginx.conf that does not work and I have no idea why. According to the documentation it should work. Part of the config looks like this.
The first rule on port 8100 works and redirects the call http://example.com/api/domains to https://localhost:8181/oan/resources/domains
# Working
server {
listen 8100 default_server;
server_name example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
root /var/www/html/example;
location /api {
proxy_pass https://localhost:8181/oan/resources; break;
}
# For ReactJS to handle routes
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ / break;
}
}
}
# Not working
server {
listen 8200;
server_name api.example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
location / {
proxy_pass https://localhost:8181/oan/resources; break;
}
}
The last call to port 8200: http://api.example.com:8200/domains SHOULD redirect to: https://localhost:8181/oan/resources/domains but does NOT do that.
What is wrong with this config and how can I get the last rule on port 8200 do the correct stuff, always redirect to https://localhost:8181/oan/resources/$uri
When you use proxy_pass with an optional URI within a prefix location block, Nginx will transform the requested URI by performing a straight text substitution.
In your case, the prefix location value is / and the optional URI value is /oan/resources. So a requested URI of /foo will be transformed into /oan/resourcesfoo.
For correct operation, both values should end with / or neither end with /.
For example:
location / {
proxy_pass https://localhost:8181/oan/resources/;
}
See this document for details.

NGINX location directives for the same location

I have a Nexus Repository Manager OSS 3.0 running behind NGINX as a private docker registry. My Docker client (not the official docker client) is expecting a 200 OK and an empty JSON string {} to be returned from /v2/. The problem i'm running into is that Nexus returns the 200 OK but an empty string.
My work-around is to have NGINX return a file containing an empty JSON string for /v2/ requests and proxy /v2/* requests to Nexus.
server {
listen 443 ssl;
server_name nexus.example.com;
ssl_certificate ssl/server.crt;
ssl_certificate_key ssl/server.key;
location = /v2/ {
root /home/ubuntu/www;
index empty.json;
}
location /v2/ {
proxy_pass http://localhost:5000;
proxy_set_header Host $http_host; # required for docker client's sake
proxy_set_header X-Real-IP $remote_addr; # pass on real client's IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 900;
}
}
I would expect this to work, but it directs all traffic (/v2/, /v2/_catalog, /v2/myimage/manifests/latest, etc) to the proxy_pass. How can I make /v2/ requests go to the location to sever the empty.json file?
So you have placed a file at /home/ubuntu/www/v2/empty.json?
The problem is that the index directive will rewrite the URI to /v2/empty.json which is then processed by the location /v2/ block.
One solution would be to create another location to match the rewritten URI and serve it as a static file:
root /home/ubuntu/www;
location = /v2/ {
index empty.json;
}
location = /v2/empty.json {
}
Another solution is to use the error_page directive. But you will still need to specify a location and root to handle the static file(s):
location = /v2/ {
return 405;
error_page 405 =200 /static/empty.json;
}
location / {
root /home/ubuntu/www;
}

Nginx lost my static images for one site in sites-enabled

I have two sites in sites-enabled for nginx:
1) project - this is essentially the top level domain - mysite.com
server {
listen 80;
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static {
alias /home/www/flask-deploy/project/static/;
}
}
2) blog - this is for a blog, that is accessible via: mysite.com:8080
server {
listen 8080;
location blog/ {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static {
alias /home/www/flask-deploy/blog/static/;
}
}
Nginx has accepted both configurations, but when I visit 1 i get all of the text, but no css, or images.
If I visit 2 i get everything working fine.
What am I doing wrong?
Thank you
For the alias directive to work correctly, the URI in the location and the URI in the alias directive should both end in / or neither end in /. The algorithm seems to substitute one string for the other.
So you should probably write:
location /static {
alias /home/www/flask-deploy/project/static;
}
However, when the last element(s) of the alias match the location, the root directive is preferred (see this):
location /static {
root /home/www/flask-deploy/project;
}
Your location blog/ looks incorrect, prefix locations should always begin with a /.

Multiple apps on nginx

I'm trying to route traffic across multiple upstream servers on nginx like so:
upstream app_a {
server unix:/tmp/app_a.sock fail_timeout=10;
# For a TCP configuration:
# server localhost:8000 fail_timeout=0;
}
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
index index.html index.htm;
server_name localhost;
root /home/ubuntu/app_a/www/staging/static;
location ~ ^/app_a/(.*)$ {
try_files $1 #proxy_to_app_a;
}
location #proxy_to_app_a {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_a;
}
Unfortunately the apps have no knowledge of full uris and expect to be sitting on root - which means i need to re-write the uri when passing to the app, which is why i thought this might work:
location ~ ^/app_a/(.*)$ {
try_files $1 #proxy_to_app_a;
}
the app works fine if location is just / (because of the aforementioned root issue), but this regex based solution doesnt seem to work. What do i need to do so the app gets / instead of app_a in the url?
Thanks
location /app_a/ {
rewrite /app_a/(.*) /$1 break;
proxy_set_header Host $http_host;
proxy_pass http://app_a;
}

Resources