nginx反向代理http重定向到https,不同的端口



假设我在同一网站上有两个不同的应用程序,但在不同的端口上运行。

端口8069上的odoo端口9443 上的portainer

我的nginx配置文件:

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;
}

在浏览器中,如果我输入http://odoo.localhost,它将被重定向到https://odoo.localhost,并且不会被传递到localhost:8069有没有一种方法可以让http到https重定向只有一个服务器条目,并且能够将其发送到特定端口?

在本例中,不再添加两个服务器块:

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;
}
}

有没有办法只让一个服务器块来完成这两个任务?我最终可能会使用6个应用程序,并且必须为http添加/更新6个服务器块,为https添加/更新六个块,这似乎是多余的。

此外,为什么";代理通行证https://localhost:8069"不工作,但proxy_passhttps://localhost:9443作品

--===========================也尝试过";Gourida说:;s的建议,通过这个配置文件,我收到了警告"nginx:[warn]冲突的服务器名称"localhost";在0.0.0.0:80被忽略";,而且根本不起作用。

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;
}

这样尝试,不要忘记添加对app2 的修改

#server 1
upstream odoo1 {
server 127.0.0.1:8069;
}
#server 2
upstream odoo2 {
server 127.0.0.1:8070;
}
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;

# Add Headers for odoo proxy mode
proxy_set_header X-Forwarded-Host www.example.com;
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://odoo1;
}
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} 
if ($host = example.com) {
return 301 https://$host$request_uri;
} 
}
server {
listen 443 ssl default_server;
server_name example.com www.example.com;
# add here your ssl_certificate path
#
#
#
}

相关内容

  • 没有找到相关文章

最新更新