如何在Ubuntu 16.04上使用Nginx设置多个https虚拟服务器块?

时间:2017-07-03 02:11:36

标签: ubuntu nginx server

我尝试过几个在线教程但没有用。 这是我的文件:

的/ etc / nginx的/位点可用/默认值:

# HTTP - redirect all traffic to HTTPS
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    return 301 https://$host$request_uri;
}

# HTTPS - proxy all requests to the Node app
server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name codytpatterson.com;

    # Use the Let's Encrypt certificates
    ssl_certificate /etc/letsencrypt/live/codytpatterson.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/codytpatterson.com/privkey.pem;

    # Include the SSL configuration from cipherli.st
    include snippets/ssl-params.conf;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://localhost:5000/;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
    }
}

/etc/nginx/sites-available/mobile.codytpatterson.com:

# HTTP - redirect all traffic to HTTPS
server {
    listen 80;
    listen [::]:80 ipv6only=on;
    return 301 https://$host$request_uri;
}

# HTTPS - proxy all requests to the Node app
server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name mobile.codytpatterson.com;

    # Use the Let's Encrypt certificates
    ssl_certificate /etc/letsencrypt/live/mobile.codytpatterson.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mobile.codytpatterson.com/privkey.pem;

    # Include the SSL configuration from cipherli.st
    include snippets/ssl-params.conf;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://localhost:6000/;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
    }
}

这两个文件都在/etc/nginx/sites-enabled/中有符号链接。当我运行sudo nginx -t我被告知我有重复的应用程序试图听同一个端口(我认为不会发生因为我只有一个服务器文件被列为默认值)我觉得我有什么东西想念我不知道。也许它与代理有关?任何有nginx关怀经验的人都能开导我吗?这是我的nginx配置文件,以防万一它也有用:

/etc/nginx/nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

如果我暂时只想要一个子域,也许还有更好的方法可以做到这一点。但最终我希望能够在这台服务器上托管多个网站,因此知道如何按照我的方式(使用单独的文件)以及在单个文件中设置其他子域将会很有帮助如果这是实现这一目标的更简单方法。

1 个答案:

答案 0 :(得分:1)

我认为问题在于您对端口80的重定向缺少server_name配置,因此它们有效地重复。尝试添加它,看看它是否有所作为。