NGINX forwarding on DO: mup issues - meteor

There have been many posts about NGINX and Meteor.js (using mup). However, none of them seemed to solve for my case. I can't get the NGINX vhost to forward it correctly to where my app lives on my DigitalOcean droplet.
Here's my mup.json:
{
"servers": [
{
"host": <IP-address>,
"username": "root",
"password": <root pwd>
}
],
"setupMongo": true,
"setupNode": true,
"nodeVersion": "0.10.36",
"setupPhantom": true,
"enableUploadProgressBar": true,
"appName": myapp,
"app": ".",
"env": {
"ROOT_URL": "http://localhost",
"PORT" : 3000,
"METEOR_ENV": "development"
},
"deployCheckWaitTime": 15
}
Then, on my droplet, I have deleted /etc/nginx/sites-enabled/default and created: /etc/nginx/sites-enabled/myapp.com.conf together with a sym-link in sites-available, that looks like:
server {
listen *:80;
server_name myapp.com;
access_log /var/log/nginx/app.dev.access.log;
error_log /var/log/nginx/app.dev.error.log;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Forwarded-For $remote_addr;
}
}
After restarting NGINX, when I locally do mup deploy it cannot deploy with the message Failed to connect to localhost port 3000: Connection refused i.e. the app cannot be connected to on the droplet.
I've been banging my head against the wall for some days, any ideas what can be the issue?

Related

Strapi admin API on subfolder hitting 404 on JS files behind nginx reverse proxy

I'm trying to setup Strapi on my DO droplet.
I have a UI for my web app running on a subdomain (from here onwards: https://blah.mywebsite.com) - it's running a Next.js instance - but I believe that is irrelevant.
I also have Strapi running on a separate node process (via PM2) both the Next.js and Strapi node instances are done via pm2 using the following ecosystem.config.js file:
module.exports = {
apps: [
{
name: "webapp",
cwd: "/root/webapp",
script: "npm",
args: "start",
env: { NODE_ENV: "production" },
},
{
name: "strapi",
cwd: "/root/webappstrapi",
script: "yarn",
args: "start",
env: {
NODE_ENV: "production",
APP_KEYS: "STRINGGOESHERE,STRINGGOESHERE",
ADMIN_JWT_SECRET: "STRINGGOESHERE",
JWT_SECRET: "STRINGGOESHERE",
API_TOKEN_SALT: "STRINGGGOESHERE",
DATABASE_NAME: "DBNAMEHERE",
DATABASE_PASSWORD: "PASSWORDHERE"
},
},
],
};
From what I can see there isn't an issue with either node process and both are running just fine.
I then follow the tutorial here ("Subfolder unified"): https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/deployment/optional-software/nginx-proxy.html#nginx-virtual-host
My Strapi config/server.js file looks like this:
module.exports = ({ env }) => ({
host: env("HOST", "0.0.0.0"),
port: env.int("PORT", 1337),
url: "https://blah.mywebsite.com/strapi",
app: {
keys: env.array("APP_KEYS"),
},
});
I have ran yarn build and run the build files via the aforementioned pm2 config above.
To setup the following URL structure:
https://blah.mywebsite.com/strapi/admin
https://blah.mywebsite.com/strapi/api
My Nginx config for the subdomain looks like this (following the strapi docs):
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name blah.mywebsite.com;
root /var/www/blah.mywebsite.com/public;
# SSL
ssl_certificate /etc/letsencrypt/live/blah.mywebsite.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/blah.mywebsite.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/blah.mywebsite.com/chain.pem;
# security
include nginxconfig.io/security.conf;
location / {
proxy_pass http://127.0.0.1:3000; # next.js
include nginxconfig.io/proxy.conf;
}
location /strapi/ {
rewrite ^/strapi/?(.*)$ /$1 break;
proxy_pass http://127.0.0.1:1337;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass_request_headers on;
}
# additional config
include nginxconfig.io/general.conf;
}
# subdomains redirect
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name *.blah.mywebsite.com;
# SSL
ssl_certificate /etc/letsencrypt/live/blah.mywebsite.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/blah.mywebsite.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/blah.mywebsite.com/chain.pem;
return 301 https://blah.mywebsite.com$request_uri;
}
# HTTP redirect
server {
listen 80;
listen [::]:80;
server_name .blah.mywebsite.com;
include nginxconfig.io/letsencrypt.conf;
location / {
return 301 https://blah.mywebsite.com$request_uri;
}
}
Now when I navigate to https://blah.mywebsite.com/strapi/admin - the HTML resolves - but I get a blank page - looking at the browser console I get:
GET blah.mywebsite.com/strapi/admin/runtime~main.67ca8ce7.js net::ERR_ABORTED 404
GET blah.mywebsite.com/strapi/admin/main.57d09928.js net::ERR_ABORTED 404
So it looks like the build JS bundle files aren't being served by my server.
Looking at the pm2 logs for my strapi node instance I only see:
[2022-09-11 18:45:03.145] http: GET /admin/ (3 ms) 200
So it looks like the requests for the JS files aren't hitting the Strapi node process - which leads me to believe Nginx isn't passing on the GET requests for the JS files...
How do I solve this?
I mentioned it in https://stackoverflow.com/a/75129704/4300071 a while ago.
You must add slash to at the end of url in url in the config/server.js
module.exports = ({ env }) => ({
host: env("HOST", "0.0.0.0"),
port: env.int("PORT", 1337),
url: "https://blah.mywebsite.com/strapi/",
app: {
keys: env.array("APP_KEYS"),
},
});
after that npm run build and now it should be work.

Status 500 when start Nuxt project by PM2

I have strange issue when I running Nuxt application by PM2 on nginx.
When I start it by "pm2 start" application is working but I have Error 500 when I try to get data from database. When I start application just by "npm start" everything is working fine.
I use "ecosystem.config.js":
module.exports = {
apps: [
{
name: 'MyAppName',
exec_mode: 'cluster',
instances: 'max',
script: './node_modules/nuxt/bin/nuxt.js',
args: 'start'
}
]
}
nginx setup (sites-available/default)
server {
listen 80;
listen [::]:80;
index index.html;
server_name my-domain.com www.my-domain.com;
location ~* ^.+\.(jpg|jpeg|png|gif)$ {
rewrite ^/_nuxt(/.*) $1 break;
root /var/www/myApplicationPath;
}
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Request URL: http://my-domain.com/api/cars/?limit=5
The error:
{status: 500, message: "Cannot read properties of undefined (reading 'limit')", name: "TypeError"}
message: "Cannot read properties of undefined (reading 'limit')"
name: "TypeError"
status: 500

Adding two separate Webpack dev server apps into subfolders using Docker and NGINX

I have app1 and app2, both running with webpack-dev-server on separate ports. The client wants the apps under the same domain in different subfolders.
domain.com/app/app1
domain.com/app/app2
I set up a docker-compose.yml file:
version: '3'
services:
proxy:
image: nginx:alpine
ports:
- '80:80'
volumes:
- './nginx.conf:/etc/nginx/conf.d/nginx.conf:ro'
Here's the nginx.conf file
upstream app1 {
server host.docker.internal:5000;
}
upstream app2 {
server host.docker.internal:5001;
}
server {
listen 80;
server_name myapp;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host localhost;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
location /app/app1 {
proxy_pass http://app1;
}
location /app/app2 {
proxy_pass http://app2;
}
}
I tried different webpack settings, but nothing seems to work. Here's the webpack.config.js for app1:
output: {
filename: 'main.js',
},
devServer: {
historyApiFallback: true,
host: '0.0.0.0',
port: 5000,
},
When I visit http://myapp/app/app1, the index.html loads fine, but I get a 404 on the main.js script. The file is accessible at http://myapp/app/app1/main.js, but inside index.html, the script points to main.js, instead of app/app1/main.js.
<script type="text/javascript" src="main.js"></script></body>
I tried changing output.publicPath in webpack, but then the script is no longer available. Is there a way to change the path inside the script tag without messing everything else up? Or how would I go about making this work when the app is running inside a subfolder?
Found the solution, no thanks to the webpack docs. historyApiFallback.index needs to point to the same location as publicPath. Also, for hot reload to work and avoid console errors, you'll need to add disableHostCheck: true. Here's my final webpack config for one of the apps:
output: {
filename: 'main.js',
publicPath: '/app/app1',
},
devServer: {
disableHostCheck: true,
historyApiFallback: {
index: '/app/app1',
},
port: 5000,
publicPath: '/app/app1',
},

How to host multiple ghost blogs on one server

I have a droplet with two domains.
Each domain has a subdomain, I am trying to setup two ghost instances on each sub domain but it is giving me a really hard time.
I have a Centos server with a LEMP stack installed.
Ghost is running on the first subdomain fine, but the second one I can see the styling of the homepage/front-end, but when I visit /ghost or /admin I get an nginx 404 error not found.
Without your Nginx config file it's difficult to answer, but i will still try, you need to configure nginx to listen for those subdomains, if you have done that successfuly, you also need to configure ghost blog config.js on each blog to have different urls and port and database.
server {
listen 80;
server_name blog1.example.com;
location / {
proxy_pass http://127.0.0.1:2368/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
}
}
server {
listen 80;
server_name blog2.example.com;
location / {
proxy_pass http://127.0.0.1:2369/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
}
}
blog1.example.com config.js
production: {
url: 'http://blog1.example.com',
mail: {},
database: {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, '/content/data/ghost1.db')
},
debug: false
},
server: {
host: '127.0.0.1',
port: '2368'
}
}
blog2.example.com config.js
production: {
url: 'http://blog2.example.com',
mail: {},
database: {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, '/content/data/ghost2.db')
},
debug: false
},
server: {
host: '127.0.0.1',
port: '2369'
}
}

NodeBB with Nginx - "This web page has a redirect loop"

I'm trying to set up NodeBB at xxx.xxx.xxx.xxx/nodebb. I have the following settings for Nginx and NodeBB:
/etc/nginx/conf.d/nodebb.conf
server {
listen 80;
server_name localhost;
location /nodebb/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:4567/;
proxy_redirect off;
# Socket.IO Support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
/usr/share/nginx/nodebb/config.json
{
"base_url": "http://`xxx.xxx.xxx.xxx",
"port": "4567",
"secret": "xxxxxx-xxxxxxxxxxx-xxxxxx-xxxxx",
"bind_address": "0.0.0.0",
"database": "mongo",
"mongo": {
"host": "127.0.0.1",
"port": "27017",
"username": "xxxxxxxx",
"password": "xxxxxxxxxxx",
"database": "xxxxxxx"
},
"bcrypt_rounds": 12,
"upload_path": "/public/uploads",
"use_port": false,
"relative_path": "/nodebb"
}
When I navigate to xxx.xxx.xxx.xxx/nodebb I get redirected to xxx.xxx.xxx.xxx/nodebb/404 with a browser error message This web page has a redirect loop. Any ideas what I'm doing wrong?
Edit: Just to say that if I navigate to http://xxx.xxx.xxx.xxx:4567/nodebb/ - it works fine
Set your server name to the same as your base_url, not localhost.
Set server_name to yourdomain.com in /etc/nginx/conf.d/nodebb.conf and the base_url to http://127.0.0.1 in /usr/share/nginx/nodebb/config.json.
If you do this you will not be able to open the nodebb site on http://yourdomain.com:4567, but on http://yourdomain.com or http://yourdomain.com:80 (which is the default port).

Resources