【问题标题】:How do I set up multiple https virtual server blocks with Nginx on Ubuntu 16.04?如何在 Ubuntu 16.04 上使用 Nginx 设置多个 https 虚拟服务器块?
【发布时间】:2017-07-03 02:11:36
【问题描述】:

我尝试了几个在线教程但无济于事。 这是我拥有的文件:

/etc/nginx/sites-available/默认:

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

如果我暂时只想要一个子域,也许还有更好的方法来做到这一点。但最终我希望能够在此服务器上托管多个网站,因此了解如何按照我的方式进行操作(使用单独的文件)以及在单个文件中设置额外的子域会很有帮助如果这是更简单的方法来实现这一点。

【问题讨论】:

    标签: ubuntu nginx server


    【解决方案1】:

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

    【讨论】:

    • 嘿@jlengstorf 谢谢你的回答!我添加了server_name 配置,我仍然得到相同的结果。此外,我尝试在 nginx.conf 中取消注释 # server_name_in_redirect off; 行,结果相同。最后,我尝试在 https 服务器块中注释掉 server_name 配置,以防在两次定义服务器名称时出现任何问题。还有其他想法或想法吗?
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 2017-04-07
    • 2021-10-15
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多