Linux系统:Ubuntu18.04

Nginx版本:1.14.0

域名:阿里云申请的域名

证书:https://freessl.cn/这里免费申请的,只要有域名可以申请无数个,每个质保一年,用完后再申请。


画了个草图,不要喷,大概是这么个意思,不要在意细节。

nginx server_name 配置多个域名 nginx配置多个http_http

 首先还是进行Nginx安装吧。安装前得确认80端口别被占用443也最好别别占用。

#比较任性啊,直接是root,如果不是很自信建议不要root,哈哈。
#先更新一下库
root@iZ2zecynwitmm7j95jyf8nZ:/# apt update
#默认安装
root@iZ2zecynwitmm7j95jyf8nZ:/# apt install nginx
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libgd3 libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libxpm4 nginx-common nginx-core
Suggested packages:
  libgd-tools fcgiwrap nginx-doc
The following NEW packages will be installed:
  libgd3 libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libxpm4 nginx nginx-common nginx-core
0 upgraded, 10 newly installed, 0 to remove and 37 not upgraded.
Need to get 750 kB of archives.
After this operation, 2,670 kB of additional disk space will be used.
Do you want to continue? [Y/n] 
#这里直接按键盘“y“,(预防新手操作不会,提示一下)
#然后安静的等待完成就好。
#完成后可以测试一下,看下状态
root@iZ2zecynwitmm7j95jyf8nZ:/# systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2021-11-22 14:31:11 CST; 46min ago
     Docs: man:nginx(8)
  Process: 20667 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=2)
  Process: 20836 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 20835 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 20837 (nginx)
    Tasks: 9 (limit: 4915)
   CGroup: /system.slice/nginx.service
           ├─20837 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ├─20838 nginx: worker process
           ├─20839 nginx: worker process
           ├─20840 nginx: worker process
           ├─20841 nginx: worker process
           ├─20842 nginx: worker process
           ├─20843 nginx: worker process
           ├─20844 nginx: worker process
           └─20845 nginx: worker process

Nov 22 14:31:11 iZ8vbd3whsqpuhzrq9w78uZ systemd[1]: Starting A high performance web server and a reverse proxy server...
Nov 22 14:31:11 iZ8vbd3whsqpuhzrq9w78uZ systemd[1]: Started A high performance web server and a reverse proxy server.
#成功了一般是这个样子。

到此,Nginx安装完毕。

在这里先把一些命令弄出来:

service nginx start    开启Nginx服务
service nginx stop    停止Nginx服务
service nginx restart    重启Nginx服务
nginx -s reload    重新加载Nginx配置文件
nginx -v    查看 Nginx的版本号
nginx -s stop    停止 nginx
nginx -s quit    退出 nginx
nginx -t    检查配置文件是否正确(这个命令很重要,修改完配置一定要执行一下,看看自己改的配置是否正确)

开始配置https

用命令安装完,一般情况下安装的版本是1.14.0,然后配置文件路径一般是:

/etc/nginx/sites-enabled/default

(没错,就是这个default文件,看了网上一大堆抄来抄去的博客,都是说修改nginx.conf。哎,因该是自己水平不到吧,反正没成功过,也不能说分享经验的大神们说的不对,只是不符合自己的需求吧。)

此时,你应该已经在我边发的白嫖网址里生成了ssl相关的证书了,记得选nginx的证书。(忘说了,得提前弄好,不然太费时间了,当初这个不好弄啊)。

证书大概是以您的域名命名的,后缀是.crt和.key这两个。还有是.pem什的么,我没遇到过,不过原理应该一样吧,可以参考一下,都是个加密文件用来检验的。有机会我试试。

#进入我的配置目录,这里看着我的这个应该是个类似于快捷方式的东西,
#指向到了/etc/nginx/sites-available/default这个目录,然后我打开看了看,果然一样……
root@iZ8vbd3whsqpuhzrq9w78uZ:/etc/nginx/sites-enabled# ll
total 8
drwxr-xr-x 2 root root 4096 Nov 22 14:31 ./
drwxr-xr-x 9 root root 4096 Nov 22 14:08 ../
lrwxrwxrwx 1 root root   34 Nov 19 17:23 default -> /etc/nginx/sites-available/default

修改完以后我的default是这样的:

其中为了避免不必要的麻烦,我把我真是的域名改成了www.myssl.cn(就是把www.myssl.cn和myssl.cn这个就是你们自己的域名,小白们这么干。)

server {
	listen 443 ssl;
	listen [::]:443 ssl ipv6only=on;

	root /var/www/html;

	server_name www.myssl.cn myssl.cn;

	ssl on;

	ssl_certificate /etc/nginx/ssl/private/ssl.crt;#证书地址
	ssl_certificate_key /etc/nginx/ssl/private/ssl.key;#证书地址

	location /one {
		proxy_pass http://localhost:8081/;
	}
	location /two {
		proxy_pass http://localhost:8082/;
	}
	location /three {
		proxy_pass http://localhost:8083/;
	}
}
server {
	listen 80;
	listen [::]:80;
	server_name myssl.cn www.myssl.cn;	
	return 301 https://www.myssl.cn$request_uri;
}

弄完后保存退出,然后一定要执行下边命令:

nginx -t

检查一下。

得到这个结果:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

说明修改的文件是正确的,没有语法错误,接下来就能用了。

这个最终的效果是:

内网地址

https地址

http://localhost:8081/ggh/XXX?param=?

https://www.myssl.cn/one/ggh/XXX?param=?

http://localhost:8082/xcx/XXX?param=?

https://www.myssl.cn/two/xcx/XXX?param=?

http://localhost:8083/account/XXX?param=?

https://www.myssl.cn/three/account/XXX?param=?

有啥子问题的可以 留言啊,看到肯定回复。qq也行啊。