Nginx setup for local webapp and websocket - linux

following is my nginx configuration,
server { //PART-1
listen 80;
server_name _;
location / {
proxy_pass http://127.0.0.1:8090;
proxy_redirect off;
proxy_pass_request_headers on;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection Upgrade;
}
}
server { //PART-2
listen 80;
server_name service;
root /usr/local/tomcat7/webapps/service-snapshot;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080/ServiceUI/;
}
}
first part of config works fine for websockets, which I am already using.
Second part of config is for webapp running on Apache tomcat 7.0.56, which is not working.
Is there something wrong with config? assuming server_name in both parts might be causing issue!
Any suggestions!

While having multiple services on one IP and port is working perfectly fine, the server_name directive is using the HOST header submitted by the client/browser. In this case, you're not supplying the header but instead asking for a specific location on the same server (you're not asking for http://_ or http://service but for http://yourserver/services from what I see in the comments).
To make it work, you have to specify the different services via locations like this:
server {
listen 80;
server_name THIS_IS_WHERE_YOUR_DOMAIN_OR_MAYBE_LOCALHOST_GOES;
location / {
proxy_pass http://127.0.0.1:8090;
proxy_redirect off;
proxy_pass_request_headers on;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection Upgrade;
}
location /Service {
root /usr/local/tomcat7/webapps/service-snapshot;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080/ServiceUI/;
}
}

Related

nginx reverse proxy nodejs subdomain

I am having a problem.
I have a node application running on :3000 on a subdomain inside of Plesk.
I have the subdomain https://xxx.flamingocams.co.uk ;
when I navigate to the subdomain it displays the default plesk page and this is the problem;
I have tried to change the port of the node application to 80 and 443 however this conflicts with plesk.
I have no issues when accessing the node application on https://xxx.flamingocams.co.uk:3000.
Now the only other thing I've seen other people attempt is a reverse proxy;
I found this example;
server {
listen 0.0.0.0:80;
server_name xxx.flamingocams.co.uk;
access_log "/var/log/nginx/xxxflam.log";
location / {
proxy_pass http://127.0.0.1:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_cache_bypass $http_upgrade;
}
}
I am running Plesk Obsidian v18.0.34_build1800210325.10 os_Ubuntu 16.04 so my question is, Where would I place this config to get the subdomain to point only to the nodejs application?
And is this config correct for what I'm trying to achieve?
I have little to no knowledge on nginx configuration my apologies
I have checked out this post and the answer says I need to add a config /etc/nginx/sites-available/yourdomain.com however I do not have the directory sites-available
response to comments // xxx.flamingocams.co.uk.conf
server {
listen 0.0.0.0:80;
server_name xxx.flamingocams.co.uk;
access_log "/var/log/nginx/xxxflam.log";
location / {
proxy_pass http://127.0.0.1:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_cache_bypass $http_upgrade;
}
}
server {
listen 0.0.0.0:443;
server_name xxx.flamingocams.co.uk;
ssl_certificate /opt/psa/var/certificates/scfZc0CwJ;
ssl_certificate_key /opt/psa/var/certificates/scfZc0CwJ;
server_name xxx.flamingocams.co.uk;
access_log "/var/log/nginx/xxxflam.log";
location / {
proxy_pass http://127.0.0.1:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_cache_bypass $http_upgrade;
}
}
In your config, the server is running only on port 80
listen 0.0.0.0:80;
With this config, the page : http://xxx.flamingocams.co.uk will display your application (the request come to the NginX proxy then is forwarded to NodeJS application on port 3000). Because there aren't any server block listen on port 443, the default Plesk screen is displayed.
In order to have your app running on https, you need to listen on port 443 on NginX, you also need to configure the SSL certificate
The config would be :
server {
listen 0.0.0.0:443;
server_name xxx.flamingocams.co.uk;
ssl_certificate path_to_your_ssl_certificate;
ssl_certificate_key path_to_your_ssl_key;
# The rest of your config is ok :)
}

Nginx Configuration for Oracle Linux Server returning/redirecting to default oracle page

webpage screenshot
Nginx configuration:
server
{
listen 80;
server_name ****;
listen [::]:80 default_server ipv6only=on;
}
server
{
listen 443;
server_name ****;
location /
{
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:8080/;
proxy_ssl_session_reuse 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;
proxy_redirect off;
}
}
server
{
listen 443;
server_name ****;
location /
{
#root /var/www/tite_staging/public/www/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:8080/;
proxy_ssl_session_reuse 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;
proxy_redirect off;
}
}
removed "return 302 https://$host$request_uri;" from http block because it causes a ERR_TOO_MANY_REDIRECTS error
May I know what the issue is here? thank you.
Running a javascript application with node server
server {
listen 80;
# catch all server names here
server_name _;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name tite.rdc.nie.edu.sg;
location / {
proxy_pass http://localhost:8080/;
proxy_set_header Host $http_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;
}
}
server {
listen 443;
server_name staging.tite.rdc.nie.edu.sg;
location / {
proxy_pass http://localhost:8088/; # <--- SEE PORT HERE
proxy_set_header Host $http_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;
}
}
Assumptions:
URL tite.rdc.nie.edu.sg running application branch A on server A
URL staging.tite.rdc.nie.edu.sg running application branch B on server A
Means that you can not run two different applications on same server behind same port. During application start-up you should get error message, something like:
Bind address already in use
Also check /etc/nginx/config folder if it does not contain another (defaults) *.conf files

nginx doesn't redirect backend

I've nginx config that route frontend app to specific port, and backend to specific route
here's my configs
server {
listen 80;
server_name test.com www.test.com;
location / {
proxy_pass http://127.0.0.1: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;
proxy_redirect off;
}
location /api {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:3000;
proxy_set_header X-Real-IP $remote_addr;
}
}
when trying to access test.com directly it works, but when trying to access test.com/api , it doesn't work, then if return to main path test.com it also doesn't work, it seems like nginx stopped working after accessing the /api

Nginx not listening to ports

I am trying to set up a Nginx as a reverse proxy to access multiple NodeJS apps running on the same server.
I have my nodeJS apps running with PM2 and it all seems fine:
My nodeJS app is the simple nodeJS app generated with express-generator, so it is supposed to be running on port 3000.
I have also set up my Nginx with the following config
server {
listen 1004;
server_name pumadashboard.com;
location / {
proxy_pass http://127.0.0.1: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;
}
}
However when I curl 127.0.0.1:1004 I get a badGateway error from Nginx. I am also not able to access pumadashboard.com from anywhere on my local network, it just loads until timeout.
What do you get if you do:
curl http://locahost:3000
This should give back a response and like that you will understand if the application started properly.
This nginx configuration works for me
upstream pumadashboard.com {
server 127.0.0.1:3010;
}
server {
listen 80;
server_name pumadashboard.com;
root <path to your node application>;
access_log /var/log/nginx/your-access.access.log;
error_log /var/log/nginx/your-error.error.log;
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
location / {
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_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_max_temp_file_size 0;
proxy_pass http://pumadashboard.com/;
proxy_redirect off;
proxy_read_timeout 240s;
}
}
Try adding
proxy_set_header X-Forwarded-Host $http_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-Port 80;
to your location / block

Multiple websites in one VPS instance with NGINX

I still struggling with configuration of nginx for multiple websites in one virtual machine.
So If I do:
server {
listen 80;
server_name example1.com;
location / {
proxy_pass http://localhost:8181;
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;
}
}
after hit example1.com my nodejs page is loaded properly. But If I try to add second server block:
server {
listen 80;
server_name example2.com;
location / {
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;
}
}
Nothing is working, also example1. So I wanna to load through example2.com default nginx location... Something like that:
server {
listen 80;
server_name example1.com;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
server {
listen 80;
server_name example2.com;
location / {
proxy_pass http://localhost:8181;
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;
}
}
But it is always redirected to nginx root location.
How can I do that? Thanks for any help!
Node.JS?
You can refer my configuration:
location / {
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-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:2368;
}
or another one
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:2387;
}
The first configuration direct to https redirect cycle.
And you said the default site, you can config something like that:
listen 80 default;
server_name xxxxx.com;

Resources