Create test server using IP address for 2 apps - nginx

I want to deploy a test server using a digital ocean droplet. I've got it up but don't know how to setup the nginx sites-available to work correctly. I've got two apps running on the server:
/var/www/html/new_app (Should use port 8080)
/var/www/html/old_app (Should use port 8081)
I don't know what I'm doing here, and have tried looking at examples but they all use domain names and not the localhost or standard IP address.
What I have currently:
/etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/new_app;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
/etc/nginx/sites-available/old
server {
listen 80;
listen [::]:80;
root /var/www/html/pottstown_old;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
proxy_pass http://localhost:8081/;
}
}
I tried adding another file for the old site, but it gave me an error:
nginx: [warn] conflicting server name "" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "" on [::]:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
However when I call up the IP address, I get Failed to open page. How do I set this up to send requests for 64.225.60.54 to the 8080 port which serves the new app, and requests for port 8081 to the old_app?
Do I just need one server with two location blocks? I just don't get it.

Not sure what kind of application you want to host but in general.
In case you need two different ports for your applications you should create two server blocks.
App NEW - listen 8080
server {
listen 8080;
listen [::]:8080;
root /var/www/html/new_app;
index index.html index.htm;
}
App OLD - listen 8081
server {
listen 8081;
listen [::]:8081;
root /var/www/html/old_app;
index index.html index.htm;
}

Related

Connection problem while hosting multiple tor sites on same device

I am trying to host two tor websites on the same device. The original one works but not the new one. I have followed the steps in here but I can't connect to the server.
I've added a mew serviceDir and Port in /etc/tor/torrc
#this site works
HiddenServiceDir /var/lib/tor/hidden_service/
HiddenServicePort 80 127.0.0.1:8080
#this one does not
HiddenServiceDir /var/lib/tor/hidden_service2/
HiddenServicePort 80 127.0.0.1:8081
and in /etc/nginx/sites-enabled/default I've added
server {
listen 8081 default_server;
listen [::]:8081 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
running nginx -t gives no error
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
I am at a loss here. Any help is appreciated!

Change port for http to https -- Nginx

Sorry for limited understanding on Nginx, Iam new to Nginx.
I have a webapp running on React and Nginx. Recently I received the SSL certificates for my website. I tried to configure the website and it worked partially. The problem is when I tried to open "https://example.com", the SSL certificates are visible here but its showing nginx default home page. While when I open "http://example.com" it shows all the webcontent.
I attempted to:
change the port from 80 to 443
Reinstall nginx.
But nothing seems to work. Here is my nginx confs at the moment:
/etc/nginx/sites-available/example.org
server {
listen 443;
listen [::]:443;
ssl on;
ssl_certificate /etc/nginx/ssl/bundle.cer;
ssl_certificate_key /etc/nginx/ssl/example.key
root /var/www/html;
server_name example.org;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
listen [::]:80;
server_name _;
return 301 https://example.org;
}
/etc/nginx/conf.d/www/example.org.conf
server {
listen 80 default_server;
server www.example.org;
location / {
root /usr/share/nginx/html;
index index.htm; index.html;
}
}
Note: I reload nginx at every new attempt.
Please help where am I going wrong.
Keeping just 1 file for config works for the above problem. I kept the "default" conf at "/etc/nginx/sites-available"

nginx: I can't access default virtual host on port 80. Instead get response from wrong server_name

I have two sites-enabled for nginx.
I have the default server:
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 / {
try_files $uri $uri/ =404;
}
}
and I have a FQDN with a redirect for https:
server {
listen SERVER-IP:80 ;
listen [::]:80;
server_name FQDN;
location / {
return 301 https://$host$request_uri;
}
}
My issue is that if I try to access my server using a different domain name or using the IP address, using http on port 80, I will always be redirected to https on port 443. I cannot seem to get the default server to respond at all.
I even created another server block that begins:
server {
listen 80;
listen [::]:80;
server_name OTHER-FQDN;
And even when I try to load http://OTHER-FQDN I get redirected to https port 443 with a certificate of FQDN.
Why?
Or better: how can I gain insight into which server block is being used for which request? Clearly only the block with FQDN is ever accessed even though I have another OTHER-FQDN that matches or a default_server that should match.
I am frustrated because the inner working of nginx in this case seem so opaque to me and counter to exectation.

Nginx: how to set up multiple servers with accept_filter=httpready

On a FreeBSD machine, I have a super-simple Nginx conf file (nginx/1.8.1):
server {
server_name .domain1.com;
listen 80 accept_filter=httpready;
index index.html;
location / {
root /home/www1;
}
}
server {
server_name domain2.com;
listen 80 default_server accept_filter=httpready;
index index.htm;
location / {
root /home/www2;
}
}
This works if I only have accept_filter=httpready enabled for one of the servers. But as it is written above, it fails, with
nginx: [emerg] duplicate listen options for 0.0.0.0:80
Why can't I set accept_filter=httpready for both servers?
http://nginx.org/r/listen
The listen directive can have several additional parameters specific to socket-related system calls. These parameters can be specified in any listen directive, but only once for a given address:port pair.
So you should use accept_filter=httpready only once, but it will be effective for all server blocks that listen to the same address:port.

Nginx 502 Bad Gateway and [error] 23645#0

I've just installed Nginx on Ubuntu 14.04. I'd like to host multiple applications on this server. Currently I have two server blocks: /etc/nginx/sites-available/default and /etc/nginx/sites-available/test. Both are symlinked to /etc/nginx/sites-enabled/. I'd like /etc/nginx/sites-available/test to serve to a subdomain I've set up and /etc/nginx/sites-available/default to serve to all other requests made to port 80. However, I get a 502 Bad Gateway error page and [error] 23645#0: *1 connect() failed (111: Connection refused) while connecting to upstream in the error log. The only way I can get /etc/nginx/sites-available/test to serve is to set the IP address explicitly in the listen directive. But in that case, all requests made to port 80 are served /etc/nginx/sites-available/test. It seems like the server_name directive is being ignored.
Here are both server blocks:
/etc/nginx/sites-available/default
server {
listen 80;
listen [::]:80;
root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
/etc/nginx/sites-available/test
server {
listen 80;
listen [::]:80;
root /var/www/test/html;
index index.html index.htm;
server_name subdomain.domain.com;

Resources