引言
恩……这篇文章的起因嘛,主要还是贫穷。
唉,哪有后端不想一个项目一台服务器的呢?无奈之举而已
实现方法
1. 有Nginx
服务器
这里指的有,是两个方面:
- 拥有一台服务器(线上线下均可,线下的话,可能需要内网穿透或转发)
- 服务器上拥有
Nginx
服务
如果没有服务器的话,可以在各大云厂商处购买,购买方法如下:
参考另一篇文章:购买阿里云ECS服务器
没有安装Nginx
服务的话,可以使用yum
安装或源码安装,源码安装教程如下:
参考另一篇文章:源码搭建Nginx服务器
2. 找到Nginx
配置文件
连接上远程服务器。
首先找到你安装的位置,比如我,按照上面教程安装的话
你的配置目录就在你安装目录下的conf
文件夹。
可以看到,里头有个nginx.conf
文件,这个文件就是我们Nginx
的主配置文件了。
用vim
命令打开它!
然后可以看到这里有很多的行,内容很多,不要紧,我们接下来捋一捋。
3. 捋一捋文件结构
这里第一行是user
,代表Nginx
服务器访问资源时用的用户信息和用户组,通常写法是:user 你的Nginx用户名 你的Nginx用户组名
后面有worker_processes
,代表工作进程数,服务不大的情况,写1就行。
后面有events
模块和http
模块。
这部分我们主要关注http
模块,我们看到标准的配置文件给了个默认的配置。
http { include mime.types; default_type application/octet-stream; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; location / { # root html; root 默认项目目录; index index.html index.htm; } location = /50x.html { root html; } } }
主要关注server
段,每个server
段,都代表监听了一个端口和主机名。
比如默认的这种,就是监听访问本机80端口
的请求,将这些请求转发到你的默认项目目录下。
那我们就可以开始仿照这种方式来写了。
4. 修改配置文件,支持引用多站点域名配置
但由于一个一个站点域名部署起来会多次修改,这里最好不要直接写在Nginx
的主配置文件nginx.conf
文件里。
这里我们用到一个新的知识点,include
方法,来将本来需要写在一个配置文件里的内容拆分到几个文件里,然后include
引用进来。
我们在server
模块的后面,加入我们的include
子句。
# more website include vhost/*.conf;
这里是在引用vhost/
文件夹下面,所有文件后缀是.conf
的文件内容,引用到当前位置。
注意:你插入include
子句的位置一定要是在server
模块后面,别弄错了
我这里为了防止你弄错,再放一遍结构:
http { include mime.types; default_type application/octet-stream; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; location / { # root html; root 你的默认项目目录; index index.html index.htm; } location = /50x.html { root html; } } # more website include vhost/*.conf; }
5. 创建站点配置存放的文件夹
刚刚上面我提到了一个vhost
文件夹,但实际上这个文件夹是不存在的。
我们需要用命令创建它,注意,位置不要搞错了,这个vhost
文件夹是跟nginx.conf
文件同级的,都是在conf
文件夹下的。
mkdir vhost
然后,我们进到这个目录下,准备创建配置文件。
6. 创建站点域名文件
这里我写的是基础范例。
如果需要在一台服务器上挂着多个站点,就需要创建多个配置文件。
在这个nginx安装目录/conf/vhost/
下,我们使用vim
命令进行编辑一个新文件。
注意:这里的文件名没有限制,但后缀一定要是.conf
,除非你上面没有按照我的说法来
建议文件名用站点的域名来。
vim hw13.cn.conf
我这里的文件名用的是我的域名,你自行更换哈。
https
强制跳转方案
# 网站 http 访问 server{ listen 80; server_name 你的域名; rewrite ^(.*)$ https://$host$1 permanent; } # 网站 https 访问 server { listen 443 ssl; server_name 你的域名; root 你的项目根目录; index index.html index.htm index.php; ssl_certificate cert/你的域名.pem; ssl_certificate_key cert/你的域名.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件。 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。 ssl_prefer_server_ciphers on; # 苹果要求的,需要可访问这个文件,如无需求可省略 location ~ /apple-app-site-association { default_type application/json; alias 你的项目根目录/apple-app-site-association; } # 所有请求全部重定向到index.php location / { if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php){ rewrite (.*) $1/index.php; } if (!-f $request_filename){ rewrite (.*) /index.php; } } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME 你的项目根目录$fastcgi_script_name; include fastcgi_params; } }
我这里是一台部署了php
的服务器,如果你是Java
的服务器,可以用别的代理端口和解析服务。
另外,这里部署了https
的配置,如果遇到有http
的访问,会直接产生一个301重定向
转发到https
的站点。
由https
进行解析处理。
仅http
解析使用方案
配置文件如下:
# 网站 http 访问 server{ listen 80; server_name 你的域名; root 你的项目根目录; index index.html index.htm index.php; # 苹果要求的,需要可访问这个文件,如无需求可省略 location ~ /apple-app-site-association { default_type application/json; alias 你的项目根目录/apple-app-site-association; } # 所有请求全部重定向到index.php location / { if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php){ rewrite (.*) $1/index.php; } if (!-f $request_filename){ rewrite (.*) /index.php; } } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME 你的项目根目录$fastcgi_script_name; include fastcgi_params; } }