Nginx二级域名配置

YOASOBI 在 2月15号出新专辑了捏

在去年Digital Ocean把我的账号突然封禁之后,我甚至没有办法迁移我的数据,所有配置文件只能重写
这里是为了留一份Nginx中二级域名.conf配置文件的写法,依据我的习惯,仅供参考

Nginx现在的版本应该会在/etc/nginx/目录存在nginx.conf
这里大概是这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

user nginx;
worker_processes auto;

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


events {
worker_connections 1024;
}


http {
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;
}

可见,nginx.conf引用了/etc/nginx/conf.d/下的所有.conf文件
这里我习惯把我的个人网站配置文件放在这

Start

现在假设我想要创建一个名为blog.romichan.me的网站,用来存放我的博客
首先在你域名的托管商配置DNS解析,可以是A解析,也可以是AAAA解析,需要绑定到服务器上
接着在/etc/nginx/conf.d/下新建一个名为blog.ssl.conf的文件,他一般是这样的:

1
2
3
4
5
6
7
8
9
10
server {
listen 443 ssl; # 自从新版本nginx开始,启用ssl隧道得这么写
ssl_certificate /etc/letsencrypt/live/blog.romichan.me/fullchain.pem; # 证书文件
ssl_certificate_key /etc/letsencrypt/live/blog.romichan.me/privkey.pem; # 证书密钥文件
server_name blog.romichan.me; #这里就是要解析到的域名,也就是你在域名托管商的DNS解析里所设置的
location / {
root /usr/share/webpage/blog; #这里是博客的根目录.据你的需求,我只需要index.html所以下面的index只写了这些
index index.html index.htm;
}
}

而后我们在新建一个文件,它的名字是blog.conf,写入这些:

1
2
3
4
5
server {
listen 80; # 不作解释,8080也行
server_name blog.romichan.me; # 解析到的网站
rewrite ^(.*)$ https://$host$1 permanent; # 重点,这里是把网页强行重定向到https,这样我们上面的证书才可以起到效果!
}

总体就这么多,如果你需要通过二级域名来转发你服务器某个端口的数据 (应该是叫反向代理) ,也是这么写,不过需要修改blog.ssl.conf的一些地方:

1
2
3
4
5
6
7
8
9
10
11
12
13
server{
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/blog.romichan.me/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/blog.romichan.me/privkey.pem;
server_name blog.romichan.me;

location / {
proxy_pass http://127.0.0.1:8080; # 这里就是转发的地址了
proxy_set_header Host $proxy_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

That’s all

这就是一个随笔

我当时在搜集这些资料的时候被很多的名字都吓到了,但其实这是一个很简单的东西,完全是一个模板化的东西,只需要记下来,以后应该都是适用的

大概就这么多,如果你看到了,希望对你有帮助.


在最后的最后,感恩。

Romi Brooks♥

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2020-2023 Romi Brooks
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信