Say I have 2 different apps on the same website, but running on different ports.
odoo on port 8069 portainer on port 9443
My nginx config file:
server {
listen 443 ssl;
listen [::]:443 ssl;
include snippets/self-signed.conf;
include snippets/ssl-params.conf;
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 _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
server {
listen 443 ssl;
listen [::]:443 ssl;
include snippets/self-signed.conf;
include snippets/ssl-params.conf;
server_name odoo.localhost;
location / {
proxy_pass http://localhost:8069; #If I put https here, and in browser also enter https, it actually will not work
}
}
server {
listen 443 ssl;
listen [::]:443 ssl;
include snippets/self-signed.conf;
include snippets/ssl-params.conf;
server_name portainer.localhost;
location / {
proxy_pass https://localhost:9443;
}
}
server {
listen 80;
listen [::]:80;
server_name localhost;
return 302 https://$server_name$request_uri;
}
In browser, if I enter http://odoo.localhost, it will get redirected to https://odoo.localhost, and will NOT get passed to localhost:8069 Is there a way to only have 1 server entry for http to https redirect, and also able to send it to a specific port?
In this example, instead of adding two more server blocks:
server {
listen 80;
server_name odoo.localhost;
location / {
proxy_pass https://localhost:8069;
}
}
server {
listen 80;
server_name portainer.localhost;
location / {
proxy_pass https://localhost:9443;
}
}
Is there a way to have only 1 server block to do the job of these two? I might eventually end up with say 6 apps, and having to add/update 6 server blocks for http, and 6 blocks for https, seems to be redundant.
Also, why "proxy_pass https://localhost:8069" not work, but proxy_pass https://localhost:9443 works?
--=========================== Also tried "Said Gourida"s suggestion, with this config file, I am getting warning "nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored", and it doesn't work at all.
upstream odoo {
server 127.0.0.1:8069;
}
upstream portainer {
server 127.0.0.1:9443;
}
#odoo
server {
listen 80;
server_name localhost;
return 301 https://localhost$request_uri;
# Add Headers for odoo proxy mode
proxy_set_header X-Forwarded-Host localhost;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
location / {
proxy_redirect off;
proxy_pass http://odoo;
}
if ($host = localhost) {
return 301 https://$host$request_uri;
}
}
#portainer
server {
listen 80;
server_name localhost;
return 301 https://localhost$request_uri;
# Add Headers for portainer proxy mode
proxy_set_header X-Forwarded-Host localhost;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
location / {
proxy_redirect off;
proxy_pass http://portainer;
}
if ($host = localhost) {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl default_server;
server_name localhost;
include snippets/self-signed.conf;
include snippets/ssl-params.conf;
}