如何使用ssl和相同的域名运行api和网站

时间:2018-05-23 11:15:02

标签: node.js ssl nginx https lets-encrypt

我有以下网站nepherius.xyz,我想使用相同名称运行常规域名和API:

https://nepherius.xyz - 这将是常规网页 https://nepherius.xyz:8433 - 这将是api

当前的nginx.conf,其中大部分是由来自letsencrypt的

的certbot生成的
server {

    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 nepherius.xyz www.nepherius.xyz_;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        proxy_pass http://localhost:8433;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/nepherius.xyz/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/nepherius.xyz/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}


server {
    if ($host = nepherius.xyz) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80 default_server;

    server_name nepherius.xyz www.nepherius.xyz_;
    return 404; # managed by Certbot


}
server {
    if ($host = www.nepherius.xyz) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80 ;
    server_name www.nepherius.xyz;
    return 404; # managed by Certbot

}

现在API工作正常但不是常规网站,如果我删除

    proxy_pass http://localhost:8433;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;

然后常规网站工作,但API没有。那么我将如何使两者都有效呢?

1 个答案:

答案 0 :(得分:1)

尝试创建两个服务器,例如:

server {
    listen       80;
    server_name  www.nepherius.xyz;
    ...
}

server {
    listen       8433;
    server_name  www.nepherius.xyz;
    ...
}

这些将有助于您在不同的端口使用您的域名。