NGINX not following alias directive - nginx

I am trying to get nginx to server some static files. I put in what I believe should be the proper directive to alias the url, but nginx is refusing to server the page. My server.conf is as follows:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm index.php;
# Make site accessible from server.fqdn.suffix
server_name server.fqdn.suffix;
location /static/ {
autoindex on;
alias /usr/share/nginx/html/poindexter/inspire/static/;
}
# Include the basic h5bp config set
include /etc/nginx/h5bp/basic.conf;
}
I assumed that this would redirect to the /usr/share/nginx/html/poindexter/inspire/static/ directory. And for the autoindex it seems to work. But when you click on a file it generates a 404 error. I took an strace and sure enough, it is not honoring the alias.
[pid 2542] <... epoll_wait resumed> {{EPOLLIN, {u32=1822502928, u64=139901792235536}}}, 512, 4294967295) = 1
[pid 2542] accept4(10, {sa_family=AF_INET, sin_port=htons(37845), sin_addr=inet_addr("198.55.232.86")}, [16], SOCK_NONBLOCK) = 24
[pid 2542] epoll_ctl(21, EPOLL_CTL_ADD, 24, {EPOLLIN|EPOLLET, {u32=1822503697, u64=139901792236305}}) = 0
[pid 2542] epoll_wait(21, {{EPOLLIN, {u32=1822503697, u64=139901792236305}}}, 512, 10000) = 1
[pid 2542] recvfrom(24, "GET /static/css/bootstrap.min.cs"..., 1024, 0, NULL, NULL) = 772
[pid 2542] open("/usr/share/nginx/html/static/css/bootstrap.min.css", O_RDONLY|O_NONBLOCK) = -1 ENOENT (No such file or directory)
[pid 2542] write(8, "2014/03/05 21:59:23 [error] 2542"..., 328) = 328
[pid 2542] writev(24, [{"HTTP/1.1 404 Not Found\r\nServer: "..., 172}, {"<html>\r\n<head><title>404 Not Fou"..., 116}, {"<hr><center>nginx</center>\r\n</bo"..., 46}], 3) = 334
[pid 2542] setsockopt(24, SOL_TCP, TCP_NODELAY, [1], 4) = 0
[pid 2542] recvfrom(24, 0x14e9240, 1024, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable
I would really appreciate any help I could get. I am very stuck.

I see several way to fix this.
1
Just add symlink from /usr/share/nginx/html/static/ to /usr/share/nginx/html/poindexter/inspire/static/ and remove alias directive. It something like aliasing on file system level.
location /static/ {
autoindex on;
}
2
Remove location ~* \.(?:css|js)$ { block.
3
Use rewrite (this solution make use the fact you static directory in inside your root)
location ^~ /static/ {
autoindex on;
rewrite ^(.+)$ /poindexter/inspire$1;
}

Related

Nginx failing to resolve upstream with custom DNS resolver

docker run --rm --net=host -v $PWD/default.conf:/etc/nginx/conf.d/default.conf nginx
2019/05/12 17:02:49 [emerg] 1#1: host not found in upstream "tickethub.service.consul" in /etc/nginx/conf.d/default.conf:10
nginx: [emerg] host not found in upstream "tickethub.service.consul" in /etc/nginx/conf.d/default.conf:10
While dig shows the DNS record correctly:
dig #127.0.0.1 -p 8600 tickethub.service.consul
; <<>> DiG 9.12.3-P1 <<>> #127.0.0.1 -p 8600 tickethub.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57394
;; flags: qr aa rd; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 4
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;tickethub.service.consul. IN A
;; ANSWER SECTION:
tickethub.service.consul. 0 IN A 172.23.0.6
tickethub.service.consul. 0 IN A 172.23.0.5
tickethub.service.consul. 0 IN A 172.23.0.7
;; ADDITIONAL SECTION:
tickethub.service.consul. 0 IN TXT "consul-network-segment="
tickethub.service.consul. 0 IN TXT "consul-network-segment="
tickethub.service.consul. 0 IN TXT "consul-network-segment="
;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Sun May 12 16:58:54 GMT 2019
;; MSG SIZE rcvd: 209
And my nginx config:
server {
listen 80;
server_name localhost;
location / {
resolver 127.0.0.1:8600;
proxy_pass http://tickethub.service.consul;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
What may be the issue?
This worked when I explicitly set the DNS on the docker container to 127.0.0.1 which means Nginx is probably trying to resolve it WITHOUT using the resolver specified argh...
I think I also had to change the DNS port to 53 instead of the explicit 8600
Or something...
Probably a bunch of nginx bugs...
mic drop
It worked when I set the proxy_pass using a variable:
location / {
resolver consul;
set $endpoint tickethub.service.consul;
proxy_pass http://$endpoint/;
}

What request path is location = /bar supposed to match in Nginx?

What request path is location = /bar supposed to match in Nginx?
What works fine: location /bar
Here is my nginx configuration, host file configuration, and my HTML files.
# cat /etc/nginx/sites-enabled/foo
server {
listen 80;
listen [::]:80;
server_name foo;
root /tmp/;
location /bar/ {
alias /var/www/foo/;
}
}
# cat /etc/hosts
127.0.0.1 localhost foo
127.0.1.1 debian
# cat /tmp/index.html
Hi! I am Tmp!
# cat /var/www/foo/index.html
<p>Hi! I am Index!</p>
# cat /var/www/foo/max.html
<p>Hi! I am Max!</p>
HTTP requests to the root, /bar/, and /bar/max.html produce the expected
output:
# systemctl restart nginx && curl http://foo/
Hi! I am Tmp!
# systemctl restart nginx && curl http://foo/bar/
<p>Hi! I am Index!</p>
# systemctl restart nginx && curl http://foo/bar/max.html
<p>Hi! I am Max!</p>
What does not work fine: location = /bar
Now I edit the configuration to replace location /bar with location = /bar:
# cat /etc/nginx/sites-enabled/foo
server {
listen 80;
listen [::]:80;
server_name foo;
root /tmp/;
location = /bar/ {
alias /var/www/foo/;
}
}
# systemctl restart nginx && curl http://foo/
Hi! I am Tmp!
These HTTP requests no longer work:
# systemctl restart nginx && curl http://foo/bar/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>
# tail -n 1 /var/log/nginx/error.log
2018/05/25 00:07:50 [error] 29157#29157: *1 open() "/tmp/bar/index.html" failed (2: No such file or directory), client: 127.0.0.1, server: foo, request: "GET /bar/ HTTP/1.1", host: "foo"
# systemctl restart nginx && curl http://foo/bar
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>
# tail -n 1 /var/log/nginx/error.log
2018/05/25 00:08:49 [error] 29203#29203: *1 open() "/tmp/bar" failed (2: No such file or directory), client: 127.0.0.1, server: foo, request: "GET /bar HTTP/1.1", host: "foo"
# systemctl restart nginx && curl http://foo/bar/max.html
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>
# tail -n 1 /var/log/nginx/error.log
2018/05/25 00:10:10 [error] 29265#29265: *1 open() "/tmp/bar/max.html" failed (2: No such file or directory), client: 127.0.0.1, server: foo, request: "GET /bar/max.html HTTP/1.1", host: "foo"
It appears that the GET requests for either /bar or /bar/ match the location = /bar/ directive? I thought these requests should have worked with this directive because http://nginx.org/en/docs/http/ngx_http_core_module.html#location mentions:
Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates.
But as explained in my example, it does not seem to work? What kind of request would match the location = /bar directive then?
The URI /bar/ relies on the index directive to internally rewrite the URI to /bar/index.html. See this document for details.
The exact match location syntax will only match the original URI, and not the rewritten URI.
nginx will process the rewritten URI using the default location (which in your configuration, is the server context). So, the URI /bar/index.html will be searched for at /tmp/bar/index.html, and not found.

nginx = / location pattern not working

I am trying to configure nginx to serve a static html page on the root domain, and proxy everything else to uwsgi. As a quick test I tried to divert to two different static pages:
server {
server_name *.example.dev;
index index.html index.htm;
listen 80;
charset utf-8;
location = / {
root /www/src/;
}
location / {
root /www/test/;
}
}
This seems to be what http://nginx.org/en/docs/http/ngx_http_core_module.html#location says you can do. But I'm always getting sent to the test site, even on the / request by visiting http://www.example.dev in my browser.
Curl output:
$ curl http://www.example.dev -v
* Rebuilt URL to: http://www.example.dev/
* Hostname was NOT found in DNS cache
* Trying 192.168.50.51...
* Connected to www.example.dev (192.168.50.51) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.37.1
> Host: www.example.dev
> Accept: */*
>
< HTTP/1.1 200 OK
* Server nginx/1.8.0 is not blacklisted
< Server: nginx/1.8.0
< Date: Tue, 19 May 2015 01:11:10 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 415
< Last-Modified: Wed, 15 Apr 2015 02:53:27 GMT
< Connection: keep-alive
< ETag: "552dd2a7-19f"
< Accept-Ranges: bytes
<
<!DOCTYPE html>
<html>
...
And the output from the nginx access log:
192.168.50.1 - - [19/May/2015:01:17:05 +0000] "GET / HTTP/1.1" 200 415 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36" "-"
So I decided to comment out the test location. So I have only the location = / { ... block. Nginx now 404s and logs the following error:
2015/05/19 01:24:12 [error] 3116#0: *6 open() "/etc/nginx/html/index.html" failed (2: No such file or directory), client: 192.168.50.1, server: *.example.dev, request: "GET / HTTP/1.1", host: "www.example.dev"
Which is the default root in the original nginx conf file? I guess this confirms my location = / pattern is not matching.
I added $uri to the access log and see that it is showing /index.html which I guess means the first location pattern is matching, but then it goes into the second location block? So now I just need to figure out how to serve my index.html from the / block, or just add another block like: location =/index.html
according to #Alexey Ten commented, in ngx_http_index doc:
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”.
In your case, request to "/" will not get /www/src/index.html, but /www/test/index.html.

Play a video from remote computer using NGINX-RTMP

I am trying to stream a video using nginx-rtmp with rtmp protocol.
I created a index.html and embedded on it JWPlayer with the rtmp url.
On localhost, I can play the video from the browser but from another computer in same local network when I tried same thing ( open the index.html in the browser the url is http://172.16.40.162:8080 ) I get an error message.
But when I try with vlc (the rtmp url : rtmp://172.16.40.162/vod/test) it worked.
Here the code of index.html
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>VoD Example</title>
<script src="http://jwpsrv.com/library/s7iNvOAyEeSMdQ4AfQhyIQ.js"></script>
</head>
<body>
<div id='videotest'></div>
<script type='text/javascript'>
jwplayer('videotest').setup({
file: 'rtmp://localhost/vod/test',
width: '50%',
aspectratio: '16:9'
});
</script>
</body>
</html>
the nginx.conf
#user nobody;
worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
server_name localhost;
# rtmp stat
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
# you can move stat.xsl to a different location
root /usr/local/nginx/html;
}
# rtmp control
location /control {
rtmp_control all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
rtmp {
server {
listen 1935;
notify_method get;
chunk_size 8192;
application vod {
allow play all;
wait_video on;
play /var/www/Videos;
push rtmp://localhost/vod/test;
}
}
}
I get these messages on :
acces.log
172.16.40.148 - - [16/Apr/2015:13:08:33 +0100] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0"
172.16.40.148 [16/Apr/2015:13:19:29 +0100] PLAY "vod" "test" "" - 382 3660906 "" "LNX 9,0,124,2" (1m 58s)
127.0.0.1 [16/Apr/2015:13:51:47 +0100] PLAY "vod" "test" "" - 483 3669724 "http://172.16.40.162:8080/" "LNX 11,2,202,457" (1m 50s)
error.log
2015/04/16 13:07:06 [notice] 3771#0: using the "epoll" event method
2015/04/16 13:07:06 [notice] 3771#0: nginx/1.5.0
2015/04/16 13:07:06 [notice] 3771#0: built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
2015/04/16 13:07:06 [notice] 3771#0: OS: Linux 2.6.32-504.12.2.el6.x86_64
2015/04/16 13:07:06 [notice] 3771#0: getrlimit(RLIMIT_NOFILE): 1024:4096
2015/04/16 13:07:06 [notice] 3772#0: start worker processes
2015/04/16 13:07:06 [notice] 3772#0: start worker process 3773
2015/04/16 13:08:41 [info] 3773#0: *3 client closed connection while waiting for request, client: 172.16.40.148, server: 0.0.0.0:8080
Information about the video I use (I executed ffmpeg -i test.flv)
Input #0, flv, from 'test.flv':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp42isom
encoder : Lavf56.30.100
Duration: 00:01:50.03, start: 0.060000, bitrate: 270 kb/s
Stream #0:0: Video: h264 (High), yuv420p, 320x240 [SAR 1:1 DAR 4:3], 30.30 fps, 30 tbr, 1k tbn, 60 tbc
Stream #0:1: Audio: mp3, 22050 Hz, stereo, s16p, 64 kb/s
In index.html replace file: 'rtmp://localhost/vod/test' with file: 'rtmp://172.16.40.162/vod/test'.
The JWPlayer Flash will request that URL from the client machine. Using localhost will obviously not work since it looks for the stream on the same computer.

How to use The uWSGI FastRouter whith Nginx?

Configuration of Nginx:
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3030;
}
uWSGI FasterRouter
uwsgi --fastrouter 127.0.0.1:3030 --fastrouter-subscription-server 127.0.0.1:3131 -M
uWSGI web-app instance
uwsgi -M --subscribe-to 127.0.0.1:3131:/ --file server.py --http :8080
then HTTP GET [server ip]:80/ no response
HTTP GET [server ip]:8080/ got response
Configuration of Nginx:
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3030;
}
uwsgi -M --file server.py -s 127.0.0.1:3030
then HTTP GET [server ip]:80/ Got response
Do as follows. Worked. Thanks roberto for help.
Configuration of Nginx:
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3030;
}
uWSGI FasterRouter
uwsgi --fastrouter 127.0.0.1:3030 --fastrouter-subscription-server 127.0.0.1:3131 -M
uWSGI web-app instance
uwsgi -M --subscribe-to 127.0.0.1:3131:[server_ip] --file server.py -s 127.0.0.1:3232
Console output
[uwsgi-subscription for pid 18957] new pool: [server_ip] (hash key: 22902)
[uwsgi-subscription for pid 18957] [server_ip] => new node: 127.0.0.1:3232
then HTTP GET [server ip]:80/ GOT response

Resources