背景
最近 618 搞活动,买了台新的服务器,所以想着在本机搞一个静态页面,然后子域名解析到静态页面,静态页面 ajax 请求到另一台服务器,如下图所示
问题
然后我卡在了怎么配置 nginx 的二级域名上,找了网上很多教程都没有但是没有实现
还原我之前的步骤
1.配置解析域名
2.搭建了一个简单的项目,通过 ip 加端口号查看是否能够访问,发现是可以的
3.配置 nginx
# 主站ssl
server {
listen 443;
server_name localhost;
ssl on;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
ssl_certificate /ssl/3098503_runnable.run.pem;
ssl_certificate_key /ssl/3098503_runnable.run.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://www.runnable.run:8080;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
# 主站
server{
listen 80;
server_name runnable.run;
rewrite ^(.*) https://$host$1 permanent;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
# 子域名
server {
listen 80;
server_name ssq.runnable.run;
location / {
proxy_pass http://127.0.0.1:8081;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
# 子域名ssl
server {
listen 443;
server_name ssq.runnable.run;
ssl on;
ssl_certificate /ssl/ssq.runnable.pem;
ssl_certificate_key /ssl/ssq.runnable.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA; ssl_session_cache shared:SSL:50m;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://127.0.0.1:8081;
proxy_http_version 1.1;
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_set_header X-Forwarded-Proto $scheme;
}
}
结果
结果是主站任然可以访问,但是访问子域名的时候发现跳转到主站了,而且 ssl 不生效