在centos7上设置certbot和nginx子域

时间:2018-09-23 16:13:03

标签: nginx centos centos7

我正在尝试在Centos7服务器上的nginx中建立一个带有一个子域(暂时)的网站,并由Let's Encrypt的certbot提供ssl加密。

我已经成功安装了nginx,我已经设置了域:example.com www.example.comci.example.com并获得了发行的证书,没有任何问题(我的浏览器说的是“安全”,它会自动从http请求重定向到https。)

现在,我想让ci.example.com代理到localhost:6500端口(我相信它被称为proxy_pass)。我试过以下: this blog post from 2014,但nginx继续提供标准的“欢迎使用Nginx页面”。

所有其他文章/教程均适用于较旧版本的nginx(不使用/etc/nginx/nginx.conf。

这里是/etc/nginx/nginx.conf

http {
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;
keepalive_timeout   65;
types_hash_max_size 2048;

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

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
    server_name  example.com www.example.com;

    root        /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/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 = www.example.com) {
    return 301 https://$host$request_uri;
} # managed by Certbot


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

if ($host = ci.example.com) {
    return 301 https://$host$request_uri;
} #added by me

    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  example.com www.example.com;
return 404; # managed by Certbot

1 个答案:

答案 0 :(得分:0)

我已经弄清楚了,并保存下来供后代使用。

在运行certbot --nginx命令之前,我没有正确设置nginx。

这是您应该做的:

通过全新安装的Centos 7

确保Centos通过sudo yum update -y是最新的

然后通过以下方式安装nginx:sudo yum install nginx -y

它是默认设置,但这可以确保它是最新的。

然后按照this指南进行操作,以使不受保护的http://ci.yoursite.comhttp://www.yoursite.com可以按需要工作。

注意-如果由于以下原因而导致无法启动nginx的问题 [emerg] open() "/usr/share/nginx/logs/ci.yoursite.access.log" failed (13: Permission denied)

使用:su -c "setenforce 0"会将SELinux设置为许可模式,并允许nginx启动。

然后遵循: This guide使“加密”和Certbot --nginx正常工作。

在此示例中,您的最终/etc/nginx/conf.d/ci.yourserver.conf应该看起来像这样以重新路由到端口6500:

server {

    server_name ci.example.com;
    access_log logs/ci.example.access.log main;

    root /var/www/ci.example.com/html;
    index index.html index.htm;

    location / {
            proxy_pass http://localhost:6500;
            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_connect_timeout 150;
            proxy_send_timeout 100;
            proxy_read_timeout 100;
            proxy_buffers 4 32k;
            client_max_body_size 8m;
            client_body_buffer_size 128k;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
    }

listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/ci.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/ci.example.com/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 = ci.example.com) {
    return 301 https://$host$request_uri;
} # managed by Certbot


    listen 80;
    listen [::]:80;

    server_name ci.example.com;
return 404; # managed by Certbot


}