Nginx Proxy path to different port with Puppet - django

If I have an nginx server managed with Puppet, how would I go about using nginx as a proxy for any path that starts with hostname.com/api, and forward it to hostname.com:8000/api?
Example puppet configuration for server:
nginx::resource::server { "${site_name}":
listen_port => 80,
www_root => "/var/www/frontend",
ssl_redirect => false,
ssl => true,
ssl_cert => "/etc/letsencrypt/live/${site_name}/fullchain.pem",
ssl_key => "/etc/letsencrypt/live/${site_name}/privkey.pem",
ssl_port => 443,
}
I tried this but it doesn't seem to be working (it still loads the react app instead of the api's front-facing template from django)
nginx::resource::location{'/api':
server => $site_name,
ssl => true,
proxy => "https://localhost:8000",
}

Related

strapi admin not running in aws

I just finished my project in Strapi and deployed in AWS, when I run my Public ipv4 :1337 says: 'server is running successully' but when I want to log in admin panel just spinning and not showing panel.
server.js
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
cron: { enabled: true},
url: env('URL', 'http://localhost'),
admin: {
auth: {
secret: env('ADMIN_JWT_SECRET', 'MY_JWT_SECRET'),
},
},
});

aws express TIMED OUT on port 443 https

I have configured Hosted zones in Route 53 with external domaine.
I have upload and deploy app express with Elastic Beanstalk.
const express = require("express")
const cors = require('cors');
const app = express()
const PORT = process.env.PORT || 8000
connection()
app.use(cors({
origin: '*'
}));
app.get('/', (req, res) => {
res.send('Hello World')
})
app.listen(PORT, () => console.log(`Listen on port ${PORT}`))
module.exports = app
I have created AWS Certificate Manager with success.
In Elastic Beanstalk > Configuration > Load balancer > add listener :
443 | HTTPS : selected my certification
When i make request with http protocol (port 80) that work.
But when i make request with https, i have error timeout.
for information my app work in Heroku with https.
EDIT:
the problem came from Hosted zones. thank for your help

Angular 9 front/corsheaders django 3 back - appending the CORS headers to the request

having the problem to find a way how to configure properly front end app in Angular 9 consuming back REST webservice. On the remote server with the static IP i run my angular dev server with (i know it is only for dev purposes but i want to get rid of this error first)
ng serve --host 0.0.0.0 --port 80
so i can access it from the outside. then i have my django app also with the dev server at
127.0.0.1:8000
In the angular service.ts file i am trying to inject the header
export class FlightService {
httpOptions = {
headers: new HttpHeaders({
'Access-Control-Request-Method':'GET',
'Access-Control-Request-Headers':'origin, x-requested-with, accept'
})
};
private endpoint = 'http://127.0.0.1:8000/services';
constructor(private http: HttpClient) { }
//GET All Services
getAllServices(): Observable<any>{
return this.http.get(this.endpoint, this.httpOptions)
}
}
In the settings.py of my django backend i have put at the top the corsheaders module:
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
and allowed all the host (after trying many things with whitelist which none of worked:
CORS_ALLOW_CREDENTIALS = False
CORS_ALLOW_METHODS = [
# 'DELETE',
'GET',
'OPTIONS'
#'PATCH',
#'POST',
#'PUT',
]
CORS_ALLOW_HEADERS = [
'accept',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
'x-forwarded-for'
]
But I fail all the time. After i added the httpOptions i get the error in the browser:
http.js:2403 Refused to set unsafe header "Access-Control-Request-Method"
and as well:
Refused to set unsafe header "Access-Control-Request-Headers"
So at least i know the Angular is trying to append it.
Please help, how to approach it? I guess the problem is on the client side. I also tried to set up Nginx with a proxy to Angular dev, but also fail to append the headers on the Nginx configuration.
These CORS headers are not for you to set, the browser will do it automatically if it detects that you are trying to access cross domain resources.

Connection refused when connecting to redis on EC2 instance

I am trying to connect to local redis database on EC2 instance from a lambda function. However when I try to execute the code, I get the following error in the logs
{
"errorType": "Error",
"errorMessage": "Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379",
"code": "ECONNREFUSED",
"stack": [
"Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379",
" at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)"
],
"errno": "ECONNREFUSED",
"syscall": "connect",
"address": "127.0.0.1",
"port": 6379
}
The security group has the following entries
Type: Custom TCP Rule
Port: 6379
Source: <my security group name>
Type: Custom TCP Rule
Port: 6379
Source: 0.0.0.0/0
My Lambda function has the following code.
'use strict';
const Redis = require('redis');
module.exports.hello = async event => {
var redis = Redis.createClient({
port: 6379,
host: '127.0.0.1',
password: ''
});
redis.on('connect', function(){
console.log("Redis client conected : " );
});
redis.set('age', 38, function(err, reply) {
console.log(err);
console.log(reply);
});
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'The lambda function is called..!!',
input: event,
redis: redis.get('age')
},
null,
2
),
};
};
Please let me know where I am going wrong.
First thing, Your lambda trying to connect to localhost so this will not work. You have to place the public or private IP of the Redis instance.
But still, you need to make sure these things
Should in the same VPC as your EC2 instance
Should allow outbound traffic in the security group
Assign subnet
Your instance Allow lambda to connect with Redis in security group
const redis = require('redis');
const redis_client = redis.createClient({
host: 'you_instance_IP',
port: 6379
});
exports.handler = (event, context, callback) => {
redis_client.set("foo", "bar");
redis_client.get("foo", function(err, reply) {
redis_client.unref();
callback(null, reply);
});
};
You can also look into this how-should-i-connect-to-a-redis-instance-from-an-aws-lambda-function
On Linux Ubuntu server 20.04 LTS I was seeing a similar error after reboot of the EC2 server which for our use case runs an express app via a cron job connecting a nodeJs app (installed with nvm) using passport.js to use sessions in Redis:
Redis error: Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
What resolved it for me, as my nodeJs app was running as Ubuntu user I needed to make that path available, was to add to the PATH within /etc/crontab by:
sudo nano /etc/crontab Just comment out the original path in there so you can switch back if required (my original PATH was set to: PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin ) and append the location of your bin you may need to refer to, in the format:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/home/ubuntu/.nvm/versions/node/v12.20.0/bin
And the error disappeared for me
// redisInit.js
const session = require('express-session');
const redis = require('redis');
const RedisStore = require('connect-redis')(session);
const { redisSecretKey } = process.env;
const redisClient = redis.createClient();
redisClient.on('error', (err) => {
console.log('Redis error: ', err);
});
const redisSession = session({
secret: redisSecretKey,
name: 'some_redis_store_name',
resave: true,
saveUninitialized: true,
cookie: { secure: false },
store: new RedisStore(
{
host: 'localhost', port: 6379, client: redisClient, ttl: 86400
}
)
});
module.exports = redisSession;

Why my request works locally and does not on heroku?

I'm running rake send_events locally and heroku run rake send_events on heroku.
values = {sendSmsRequest:
{
from: "ABC",
to: "5581999999999",
msg: "msg",
callbackOption: "NONE",
id: "c_1541"
}
}
headers = {
:content_type => 'application/json',
:authorization => 'Basic xxxxxxxxxxxxxxxxxxxxxxxx',
:accept => 'application/json'
}
RestClient.post 'https://api-rest.zenvia360.com.br/services/send-sms', values.to_json, headers
The log print
RestClient::Exceptions::OpenTimeout
Thanks.
OpenTimeout means that rest-client timed out trying to open a connection to the server.
Are you sure that the server is up and reachable from heroku? I'm not able to reach it from my computer or any server that I tried with.
$ nc -w 10 -v api-rest.zenvia360.com.br 443
nc: connect to api-rest.zenvia360.com.br port 443 (tcp) timed out