NGINX does not serve js and css files while serving html - nginx

I installed NGINX as a reverse proxy on ubunty. However after installation it turned out that NGINX does not serve css and js files while still serving html files.
I have the following configuration in /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
location / {
proxy_pass http://localhost:5000/;
include /etc/nginx/mime.types;
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;
try_files $uri $uri/ =404;
}

Related

Stripe webhook doesn't reach endpoint on reverse proxy

I am hosting my app in Linode and I have a stripe webhook that doesn't reach the api. I could hit the endpoint before installing ssl certificates but after the ssl configuration my app can't reach the endpoint. I am following tutorials and don't know a lot about nginx proxying and backend so apologies if my explanation is not clear.
My stripe webhook is listening to the following endpoint:
https://mywebsite.com/api/stripe-webhook
And here is my proxy configuration:
server {
listen 80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
server {
# SSL configuration
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
#
#ssl on;
ssl_certificate /etc/ssl/certs/kk/certificate.crt;
ssl_certificate_key /etc/ssl/certs/kk/private_key.key;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name mywebsite.com;
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;
}
location /api {
proxy_pass http://localhost:8080;
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;
}
}
I am not even getting any meaningful error message in Stripe:
My question is what am I doing wrong and is there any other file that I need to configure?

Appwrite with Nginx

I installed Appwrite on an debian-server.
The https-port for Appwrite is 444 (443 was already used). Nginx redirects my subdomain to this port.
I have a custom SSL-certificate which is working for this domain and subdomains. I can open the appwrite via the subdomain but when I click "Sign Up" to create a root account for appwrite, I get the following Error:
Invalid Origin. Register your new client (appwrite.domain.de) as a
new Web platform on your project console dashboard
First I thought I have to set proxy_set_header Host $host; in the server-config, but then I am not able to open Appwrite... instead I get the Error
{"message":"Error: Server Error","code":500,"version":"1.0.1"}
Does someone has another idea or already fixed the same problem?
This is my Server-configuration in Nginx:
server {
server_name appwrite.domain.de;
location / {
proxy_pass https://localhost:444;
}
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/domain.de_ssl_certificate.cer;
ssl_certificate_key /etc/nginx/ssl/domain.de_private_key.key;
}
server {
listen 80;
server_name domain.de
www.domain.de
;
return 301 https://$host$request_uri;
}
server {
listen 80;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/domain.de_ssl_certificate.cer;
ssl_certificate_key /etc/nginx/ssl/domain.de_private_key.key;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name domain.de
www.domain.de
;
location / {
try_files $uri $uri/ =404;
}
Thanks for the help ;)
You're right, you need to include the proxy_set_header Host $host; directive. You might also want to include the following under server:
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_http_version 1.1;
and the following under location:
add_header X-Served-By $host;
proxy_set_header X-Forwarded-Scheme $scheme;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $port;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass $forward_scheme://$server:$port$request_uri;
If you're seeing a 500 error, it would be best to check the docker logs for the appwrite container to see what the problem is.
On a side note, if you're looking for an easier way to manage Nginx, I highly recommend Nginx Proxy Manager (NPM). I use NPM in front of my Appwrite.

multiple location in nginx is not working - error 404

I am trying to run two nodejs app one on default path (/) and another on /api , I have tried multiple ways but multiple locations are not working with nginx , only root domain (/) works fine, it doesn't matter which app I do assign (2nd or first). and if I try to visit (/api) it return 404 not found and (/) path is working fine.
Here is my default nginx file
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name example.com;
location /api {
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;
try_files $uri $uri/ =404;
}
location / {
proxy_pass http://localhost:8081;
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;
try_files $uri $uri/ =404;
}
}
What I am doing wrong. Any help will be apricated. thanks
After Doing a lot of googling and tried many ways I have found the solution and instead of using react app as dynamic loading i generated a build of react and stored that build on a particular location and served these build files on main location with nginx.
location / {
root /home/user/gui/build;
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://localhost:3002/;
proxy_redirect off;
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;
}
location /static/ {
root /home/user/gui;
try_files
/gui/build$uri
=404;
}
So here (/) will try files in the root folder which is here (/home/user/gui/build) and we have index.html which is generated by react build this index.html will be served. Now the next problem was to serve react generated static files so i have created a separate location (/static/) with the same logic of / path.

Nodejs with reverser proxy

I'm currently using NodeJs over Nginx for hosting a website, since I've already realized the port conflict of Nginx, I already change the configure file and I can access localhost now.
But the problem is the website is no longer the same as the one hosted on port 3000(which I set the NodeJs source to listen)
It is really struggling and I really do wish someone can get me a solution.
The configure code is list below
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
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;
}

Nginx doesn't load css on Asp.net core in raspberry

First of all, thanks for reading my thread. Second, sorry for my English.
I'm trying to run an Asp.net core application in a raspberry but still facing some problems when proxying with nginx.
Basically I follow these tutorials to configure my workplace and export my project to arm, the only difference is that I've created a MVC application instead a simple Hello World:
http://www.protosystem.net/blog/aspnet-core-on-raspberry-pi/
http://www.protosystem.net/blog/aspnet-core-hello-world-on-raspberry-pi/
The problem starts when I execute the app. For default, it runs on port 5000, but when nginx do the proxy for port 80 it doesn't load the css.min, javascrip files and the Home directory according to the image below:
Follow the nginx configuration below:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /www/mywebsite;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
try_files $uri $uri/ =404;
}}
Per my comment I would suggest removing the try_files directive like so:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /www/mywebsite;
index index.html index.htm index.nginx-debian.html;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}}
}

Resources