openresty why does access_by_lua_file call twice when accessing the root directory - openresty

When I use openresty to monitor IP through Lua monitor, why does access_by_lua_file call twice when accessing the root directory
Here's how I use it:
http {
access_by_lua_file lua/test.lua;
server{
location / {
default_type text/html;
}
}
}

https://nginx.org/en/docs/http/ngx_http_index_module.html
It should be noted that using an index file causes an internal redirect
That is, the request to the root (/) is internally redirected to the /index.html.
Here is a demo:
http {
access_log /dev/stdout;
access_by_lua_block {
ngx.log(ngx.INFO, ngx.var.uri, ' ', ngx.req.is_internal())
}
server {
listen 8888;
location / {
default_type text/html;
}
}
}
curl localhost:8888/index.html:
2020/08/17 15:14:22 [info] 22411#22411: *5 [lua] access_by_lua(nginx.conf:15):2: /index.html false, client: 127.0.0.1, server: , request: "GET /index.html HTTP/1.1", host: "localhost:8888"
127.0.0.1 - - [17/Aug/2020:15:14:22 +0300] "GET /index.html HTTP/1.1" 200 14 "-" "curl/7.68.0"
curl localhost:8888/:
2020/08/17 15:15:31 [info] 22411#22411: *6 [lua] access_by_lua(nginx.conf:15):2: / false, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8888"
2020/08/17 15:15:31 [info] 22411#22411: *6 [lua] access_by_lua(nginx.conf:15):2: /index.html true, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8888"
127.0.0.1 - - [17/Aug/2020:15:15:31 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/7.68.0"

Related

nuxt-mail fails with a 504 with nginx reverse proxy

I configured nuxt-mail to send emails from our nuxt app.
The baseURL of my app is changed to "https://localhost:3000/app" instead of "https://localhost:3000"
So, nginx redirects all calls to '/' to a static app. And all calls to '/app' to a dynamic app.
The issue is that on production, nuxt-mail is unable to send email through a post to '/app/mail/send'.
I tried with setting axios baseURL on nuxt.config.js as suggested on the nuxt-mail npm/github page
I don't see a path to send or mail in .nuxt/router.js
file: contact.vue
Note: WEBSITE_DOMAIN points to https://localhost:3000 locally and valid web domain on production in this format: https://www.production_website.com
<script>
...
methods: {
...
sendMail(){
this.$axios.post(
this.$config.WEBSITE_DOMAIN+'/app/mail/send',
{
...
}
...
}
...
</script>
file: nuxt.config.js
...
export default{
...
router: {
base: '/app/'
},
...
}
Note: I did configure the upstream logs from nginx to app server
Access log from nginx on production
49.205.150.249 - - [04/May/2022:15:30:54 +0000] "POST /app/mail/send HTTP/1.1" 504 167 "https://www.<xxxxxxxxx_NAME>.com/app/contact"
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:99.0) Gecko/20100101
Firefox/99.0"
Error log from nginx on production
2022/05/04 15:30:54 [error] 2106#2106: *38 upstream timed out (110:
Connection timed out) while reading response header from upstream,
client: 49.205.150.249, server: <xxxxxxxxx_NAME>.com, request: "POST
/app/mail/send HTTP/1.1", upstream:
"https://<xxxxxxxxx_IP>:3000/app/mail/send", host:
"www.<xxxxxxxxx_NAME>.com", referrer:
"https://www.<xxxxxxxxx_NAME>.com/app/contact"
What am I missing here? It works perfectly on my staging though.
The port allowing SMTP on the production instance was not open. On AWS EC2, I needed to enable outbound rules on the corresponding security group.

"handshake failed" error when using openresty (resty.http)

I am trying to make a call to a HTTPS upstream server in lua code in openresty. Code is as below:
local http = require "resty.http"
local M = {}
function M.makeHttpCall()
local httpc = http.new()
local res, err = httpc:request_uri("https://<url>", {
method = "GET",
ssl_verify = false
})
if not res then
ngx.status = 500
ngx.say("Error: ", err)
return
end
.....
end
nginx.conf has the following relevant configuration:
server {
listen 8080;
resolver local=on ipv6=off;
.....
location /httpcall {
default_type 'application/json';
content_by_lua_block {
require("../lua-modules/http")["makeHttpCall"]()
}
}
Following error is logged in error.log:
SSL_do_handshake() failed (SSL: error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure:SSL alert number 40), client: 127.0.0.1, server: , request: "GET /httpcall HTTP/1.1", host: "localhost:8080"
http.lua:15: makeHttpCall(): **request failed: handshake failed**, client: 127.0.0.1, server: , request: "GET /httpcall HTTP/1.1", host: "localhost:8080"
HTTP endpoint can be called successfully.
Any pointers to resolve the issue please?
Thanks!

Trying to run npm run serve (vue-cli app) inside Laravel Homestead (VirtualBox + Vagrant)

I have a vue-cli app, trying to run it inside Laravel Homestead.
What I have:
My hosts on host machine:
127.0.0.1 localhost
127.0.1.1 PC
192.168.2.10 myvueapp.local
hosts inside VM:
127.0.0.1 localhost
127.0.0.1 myvueapp.local
127.0.1.1 homestead homestead
Vagrant version: 2.2.4, Homestead: v8.3.2, vue --version: 3.7.0
npm run serve executes without problems inside VM, but I get
We're sorry but myvueapp doesn't work properly without JavaScript
enabled. Please enable it to continue.
as a response body from request:
//response headers
Request URL: https://myvueapp.local/
Request Method: GET
Status Code: 200
Remote Address: 192.168.2.10:443
Referrer Policy: no-referrer-when-downgrade
Browser page is blank.
Also there is one favicon request:
Request URL: https://myvueapp.local/%3C%=%20BASE_URL%20%%3Efavicon.ico
Request Method: GET
Status Code: 400 Bad Request
Remote Address: 192.168.2.10:443
Somehow BASE_URL doesn't compile in index.html:
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
My vue.config.js:
module.exports = {
devServer: {
host: 'myvueapp.local',
https: true
}
}
Homestead.yaml:
ip: "192.168.2.10"
#...
sites:
- map: myvueapp.local
to: /home/vagrant/path/to/myvueapp.local/public
#...
ports:
- send: 8080
to: 80
Port, where Vue is served (inside VM, 8080) is listening.
lsof -i :8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 3022 vagrant 22u IPv4 31440 0t0 TCP localhost:http-alt (LISTEN)
Nginx config:
server {
listen 80;
listen 443 ssl http2;
server_name .myvueapp.local;
root "/path/to/myvueapp.local/public";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
P. S. It runs ok when I'm serving it from my host machine.
What I've tried:
change host
module.exports = {
devServer: {
host: '0.0.0.0', //<-- here
https: true
}
}
, didn't helped.
Edit #1
I moved a bit further, this Nginx config now allowes me to access Vue app, served inside VM, using host machine:
location / {
try_files $uri $uri/ /index.html =404;
proxy_pass http://localhost:8080; #<-- this might be the output from npm run serve, without last slash
# App running at:
# - Local: http://localhost:8080/
# ^^^^^^^^^^^^^^^^^^^^^
}
But there is still a problem: hot-reload doesn't work.

traefik does not forward requests to a "normal" (non-docker) backend

Tested traefik in docker mode - everything goes fine. Now I need to use a "normal" backend, means forward requests from port 88 which is controlled by traefik to port 8080. But it does not work as expected.
curl -v -H Host:myhost 127.0.0.1:88 (not found, expected whoami answer)
$ curl -v -H Host:myhost 127.0.0.1:88
* Rebuilt URL to: 127.0.0.1:88/
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 88 (#0)
> GET / HTTP/1.1
> Host:myhost
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Content-Type: text/plain; charset=utf-8
< X-Content-Type-Options: nosniff
< Date: Fri, 12 Jan 2018 09:13:27 GMT
< Content-Length: 19
<
404 page not found
* Connection #0 to host 127.0.0.1 left intact
traefik is executed as ./traefik2 --logLevel=DEBUG --debug -c traefik.toml
backend is sudo docker service create -d --name whoami --constraint=node.role==manager --publish 8080:80 --replicas 1 emilevauge/whoami
Any idea?
traefik.toml
debug=true
logLevel = "DEBUG"
[traefikLog]
filePath = "tl.txt"
[accessLog]
filePath = "al.txt"
[entryPoints]
[entryPoints.http]
address = ":88"
[frontends]
[frontends.frontend1]
backend = "backend1"
[frontends.frontend1.routes.backend1]
rule = "Host:myhost"
[backends]
[backends.backend1]
[backends.backend1.servers.server1]
url = "http://127.0.0.1:8080"
curl 127.0.0.1:8080 (docker emilevauge/whoami, works as expected)
$ curl 127.0.0.1:8080
Hostname: 9134668598ed
IP: 127.0.0.1
IP: 10.255.0.7
IP: 10.255.0.8
IP: 172.18.0.3
GET / HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: curl/7.47.0
Accept: */*
$ cat al.txt
192.168.99.1 - - [12/Jan/2018:09:03:39 +0000] "GET / HTTP/1.1" - - - "curl/7.57.0" 1 - - 0ms
192.168.99.1 - - [12/Jan/2018:09:04:03 +0000] "GET / HTTP/1.1" - - - "curl/7.57.0" 2 - - 0ms
192.168.99.1 - - [12/Jan/2018:09:12:19 +0000] "GET / HTTP/1.1" - - - "curl/7.57.0" 3 - - 0ms
127.0.0.1 - - [12/Jan/2018:09:13:27 +0000] "GET / HTTP/1.1" - - - "curl/7.47.0" 4 - - 0ms
$ cat tl.txt
time="2018-01-12T09:03:35Z" level=info msg="Using TOML configuration file /home/cluster/traefik.toml
"
time="2018-01-12T09:03:35Z" level=info msg="Traefik version v1.5.0-rc4 built on 2018-01-04_02:28:22P
M"
time="2018-01-12T09:03:35Z" level=info msg="
Stats collection is disabled.
Help us improve Traefik by turning this feature on :)
More details on: https://docs.traefik.io/basic/#collected-data
"
time="2018-01-12T09:03:35Z" level=debug msg="Global configuration loaded {"LifeCycle":{"RequestAccep
tGraceTimeout":0,"GraceTimeOut":0},"GraceTimeOut":0,"Debug":true,"CheckNewVersion":true,"SendAnonymo
usUsage":false,"AccessLogsFile":"","AccessLog":{"file":"al.txt","format":"common"},"TraefikLogsFile"
:"","TraefikLog":{"file":"tl.txt","format":"common"},"LogLevel":"DEBUG","EntryPoints":{"http":{"Netw
ork":"","Address":":88","TLS":null,"Redirect":null,"Auth":null,"WhitelistSourceRange":null,"Compress
":false,"ProxyProtocol":null,"ForwardedHeaders":{"Insecure":true,"TrustedIPs":null}}},"Cluster":null
,"Constraints":[],"ACME":null,"DefaultEntryPoints":["http"],"ProvidersThrottleDuration":2000000000,"
MaxIdleConnsPerHost":200,"IdleTimeout":0,"InsecureSkipVerify":false,"RootCAs":null,"Retry":null,"Hea
lthCheck":{"Interval":30000000000},"RespondingTimeouts":null,"ForwardingTimeouts":null,"Web":null,"D
ocker":null,"File":null,"Marathon":null,"Consul":null,"ConsulCatalog":null,"Etcd":null,"Zookeeper":n
ull,"Boltdb":null,"Kubernetes":null,"Mesos":null,"Eureka":null,"ECS":null,"Rancher":null,"DynamoDB":
null,"ServiceFabric":null,"Rest":null,"API":null,"Metrics":null,"Ping":null}"
time="2018-01-12T09:03:35Z" level=info msg="Preparing server http &{Network: Address::88 TLS:<nil> R
edirect:<nil> Auth:<nil> WhitelistSourceRange:[] Compress:false ProxyProtocol:<nil> ForwardedHeaders
:0x1cb52950} with readTimeout=0s writeTimeout=0s idleTimeout=3m0s"
time="2018-01-12T09:03:35Z" level=info msg="Starting server on :88"
Kindly solved by Idez. Config must be like this ([file] section was missed):
defaultEntryPoints = ["http"]
debug=true
logLevel = "DEBUG"
[traefikLog]
filePath = "tl.txt"
[accessLog]
filePath = "al.txt"
[entryPoints]
[entryPoints.http]
address = ":88"
[file]
[backends]
[backends.backend1]
[backends.backend1.servers.server1]
url = "http://127.0.0.1:8080"
[frontends]
[frontends.frontend1]
backend = "backend1"
[frontends.frontend1.routes.test_1]
rule = "Host:myhost"

nginx reverse prroxy gives 404s on everything

This project is generally served with apache but I want to introduce nginx as a front controller to proxy requests through to memcached or fall back to apache if the URI is not found as a key in memcached.
What is happening when I make the request through nginx is I get 404s on every asset. I can paste a single asset URL from a request right in the URL bar and retrieve it, but with a 404 status. The 404s cause most of the page not to render but it seems the assets are being downloaded.
I can make the same request straight through apache and it works perfectly.
Here is my nginx config:
upstream memcached-upstream {
server 127.0.0.1:11211;
}
upstream apache-upstream {
server 127.0.0.1:5678;
}
server {
listen 4567;
root /vagrant;
server_name sc;
index index.php;
access_log /var/log/nginx/www.sc.com.access.log;
error_log /var/log/nginx/www.sc.com.error.log error;
location / {
# Only use this method for GET requests.
if ($request_method != GET ) {
proxy_pass http://apache-upstream;
break;
}
# Attempt to fetch from memcache. Instead of 404ing, use the #fallback internal location
set $memcached_key $request_uri;
memcached_pass memcached-upstream; # Use an upstream { } block for memcached resiliency
default_type application/json; # Our services only speak JSON
error_page 404 = #fallback;
}
location #fallback {
proxy_pass http://apache-upstream;
}
}
here is a sample from my nginx access log:
10.0.2.2 - - [18/Dec/2013:23:50:08 +0000] "GET /templates/assets/js/csrf.js HTTP/1.1" 404 545 "http://localhost:4567/templates/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"
And the same request from the apache log:
www.sc.com:80 127.0.0.1 - - [18/Dec/2013:23:50:08 +0000] "GET /templates/assets/js/csrf.js HTTP/1.0" 200 857 "http://localhost:4567/templates/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"
Any help would be much appreciated.
try replacing the error_page with this
error_page 404 =200 #fallback;