如何使用nginx反向代理不同子域下同一后端的两个不同端口?

时间:2016-09-23 08:27:19

标签: nginx proxy

我有一个工作配置反转代理两个应用程序:app1和app2;分别部署在两个后端服务器上:server1:8000server2:8081。在子域名下:app1.domain.comapp2.domain.com;通过Http通过TLS服务。

我的ngingx.conf文件如下:

user  nginx;
worker_processes  1;

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


events {
    worker_connections  1024;
}


http {
    server_names_hash_bucket_size 128;
    include       /etc/nginx/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;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
daemon off;

conf.d文件夹包含default.conf文件,其配置如下:

map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  default $http_x_forwarded_proto;
  ''      $scheme;
}
map $http_upgrade $proxy_connection {
  default upgrade;
  '' close;
}
gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
log_format vhost '$host $remote_addr - $remote_user [$time_local] '
                 '"$request" $status $body_bytes_sent '
                 '"$http_referer" "$http_user_agent"';
access_log off;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
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 $proxy_x_forwarded_proto;
# Mitigate httpoxy attack (see README for details)
proxy_set_header Proxy "";
server {
        server_name _;
        listen 80;
        access_log /var/log/nginx/access.log vhost;
        return 503;
}
upstream app1.domain.com {
                        server server1:8000;
}
server {
        server_name app1.domain.com;
        listen 80 ;
        access_log /var/log/nginx/access.log vhost;
        return 301 https://$host$request_uri;
}
server {
        server_name app1.domain.com;
        listen 443 ssl http2 ;
        access_log /var/log/nginx/access.log vhost;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-S$
        ssl_prefer_server_ciphers on;
        ssl_session_timeout 5m;
        ssl_session_cache shared:SSL:50m;
        ssl_session_tickets off;
        ssl_certificate /etc/nginx/certs/app1.domain.com.crt;
        ssl_certificate_key /etc/nginx/certs/app1.domain.com.key;
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
        location / {
                proxy_pass http://app1.domain.com;
        }
}
upstream app2.domain.com {
                        server server2:8081;
}
server {
        server_name app2.domain.com;
        listen 80 ;
        access_log /var/log/nginx/access.log vhost;
        return 301 https://$host$request_uri;
}
server {
        server_name app2.domain.com;
        listen 443 ssl http2 ;
        access_log /var/log/nginx/access.log vhost;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-S$
        ssl_prefer_server_ciphers on;
        ssl_session_timeout 5m;
        ssl_session_cache shared:SSL:50m;
        ssl_session_tickets off;
        ssl_certificate /etc/nginx/certs/app2.domain.com.crt;
        ssl_certificate_key /etc/nginx/certs/app2.domain.com.key;
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
        location / {
                proxy_pass http://app2.domain.com;
        }
}

我需要添加第三个应用:app3;通过TLS上的Http在server2:8082上反向代理app3.domain.com。正是这种配置让我无法工作。

我尝试了以下内容:

upstream app3.domain.com {
                        server server2:8082;
}
server {
        server_name app3.domain.com;
        listen 80 ;
        access_log /var/log/nginx/access.log vhost;
        return 301 https://$host$request_uri;
}
server {
        server_name app3.domain.com;
        listen 443 ssl http2 ;
        access_log /var/log/nginx/access.log vhost;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-S$
        ssl_prefer_server_ciphers on;
        ssl_session_timeout 5m;
        ssl_session_cache shared:SSL:50m;
        ssl_session_tickets off;
        ssl_certificate /etc/nginx/certs/app3.domain.com.crt;
        ssl_certificate_key /etc/nginx/certs/app3.domain.com.key;
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
        location / {
                proxy_pass http://app3.domain.com;
        }
}

但是当我将此配置附加到/conf.d/default.conf并测试配置时,我收到以下错误:

$ nginx -t -c /etc/nginx/nginx.conf                                                                                                                                       
2016/09/23 08:25:36 [emerg] 34#34: unexpected end of file, expecting ";" or "}" in /etc/nginx/conf.d/default.conf:120
nginx: [emerg] unexpected end of file, expecting ";" or "}" in /etc/nginx/conf.d/default.conf:120
nginx: configuration file /etc/nginx/nginx.conf test failed

0 个答案:

没有答案