欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Nginx反向代理多域名的HTTP和HTTPS服务的实现

程序员文章站 2022-06-25 18:30:46
当前nginx已经反向代理了两个网站,分别是基于windows的iis和linux的apach服务器,提供网页服务。现在有新项目的网页需要对外提供服务,需要在代理服务器上增加另外一个网站,使用http...

当前nginx已经反向代理了两个网站,分别是基于windows的iis和linux的apach服务器,提供网页服务。

现在有新项目的网页需要对外提供服务,需要在代理服务器上增加另外一个网站,使用https访问以及http自动跳转https。由于新网页是静态页面,所以使用docker部署在nginx代理服务器上。相关的certificates是通过let's encrypt来获取的,都是单独的证书,没有申请通配符形式的证书。

在nginx代理端部署ssl证书即可,后端不需要部署ssl也可以实现https访问。

Nginx反向代理多域名的HTTP和HTTPS服务的实现

nginx代理服务器配置:

worker_processes auto;

error_log /var/log/nginx/error.log warn;
pid  /var/run/nginx.pid;

events {
 worker_connections 1024;
}

http {
 include  /usr/local/nginx/conf/mime.types;
 default_type application/octet-stream;

 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
      '$status $body_bytes_sent "$http_referer" '
      '"$http_user_agent" "$http_x_forwarded_for"';

 access_log /var/log/nginx/access.log main;

 sendfile  on;
 tcp_nopush  on;
 tcp_nodelay  on;

upstream dx.exzel.co.nz {

 server 127.0.0.1:8080;
}

upstream mybusiness.exzel.co.nz {

 server 192.168.1.252:443;
}

server {
 listen 80;
 server_name dx.exzel.co.nz;
 rewrite ^(.*) https://dx.exzel.co.nz permanent;
}
server {
 listen 443;
 server_name dx.exzel.co.nz;
 ssl on;
 ssl_certificate /etc/letsencrypt/live/dx.exzel.co.nz/fullchain.pem;
 ssl_certificate_key /etc/letsencrypt/live/dx.exzel.co.nz/privkey.pem;
 ssl_session_timeout 5m;
 ssl_ciphers ecdhe-rsa-aes128-gcm-sha256:ecdhe:ecdh:aes:high:!null:!anull:!md5:!adh:!rc4;
 ssl_protocols tlsv1 tlsv1.1 tlsv1.2;
 ssl_prefer_server_ciphers on;
 access_log /var/log/nginx/ccieerror.log ;
 location / {
   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_pass http://dx.exzel.co.nz;

 }
}

server {
 listen 80;
 server_name mybusiness.exzel.co.nz;
 rewrite ^(.*) https://mybusiness.exzel.co.nz permanent;
}
server {
 listen 443;
 server_name mybusiness.exzel.co.nz;
 ssl on;
 ssl_certificate /etc/letsencrypt/live/mybusiness.exzel.co.nz/fullchain.pem;
 ssl_certificate_key /etc/letsencrypt/live/mybusiness.exzel.co.nz/privkey.pem;
 ssl_session_timeout 5m;
 ssl_ciphers ecdhe-rsa-aes128-gcm-sha256:ecdhe:ecdh:aes:high:!null:!anull:!md5:!adh:!rc4;
 ssl_protocols tlsv1 tlsv1.1 tlsv1.2;
 ssl_prefer_server_ciphers on;
 access_log /var/log/nginx/mybusiness.log ;
 location / {
   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_pass https://mybusiness.exzel.co.nz;

 }
}

server {
 listen 80;
 server_name www.empnz.co.nz empnz.co.nz;
  location / {
    proxy_pass http://192.168.1.15 ;
    }
 }

}

到此这篇关于nginx反向代理多域名的http和https服务的实现的文章就介绍到这了,更多相关nginx反向代理http和https内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!