阿里云部署 5.域名、DNS、nginx

域名

虽然经过ip地址能够直接访问你的网站,可是咱们不多看到网站是经过ip地址访问的,通常网站都会提供域名。域名直观易记,且在用户访问的域名不改变的状况下,解析的ip地址能够更改。html

域名申请能够去万网购买,国家规定了网站须要备案才能够访问,我在阿里云走的备案流程,在阿里云APP填写、上传相应信息便可,之前的流程须要邮寄幕布什么的了,如今只须要在线上操做便可。阿里云的流程走得是比较快的,最终会提交到管局,管局流程就须要等待一阵子了(5~20个工做日)。个人域名从开始备案到备案成功,流程约走了2周。node

DNS

域名须要解析到ip地址,DNS作的就是这个工做。它能够将易于管理识别的域名转换为计算机用于互连通讯的数字IP地址,从而将用户的访问路由到相应的网站或应用服务器。linux

一样,我使用了阿里云的DNS解析服务。在云解析DNS域名控制台-域名解析,点击添加域名。

再添加解析

具体配置以下
nginx

配置完成后访问域名就会解析到记录值所填写的ip地址了。c++

nginx

虽然域名解析到了ip地址(服务器),可是默认的端口是80(因此请确保服务器的80端口设置了安全组规则,具体能够参考以前的文章介绍过的方法)。咱们通常服务监听的端口不是80怎么办?其实很简单,咱们只须要简单的配置一下nginx就能够了。
接下介绍一下在阿里云服务器上安装和使用nginx。正则表达式

安装

首先安装PCRE pcre-devel 和Zlib
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括perl兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,因此须要在linux上安装 pcre库,pcre-devel是使用pcre开发的一个二次开发库。nginx也须要此库。命令:vim

yum install -y pcre pcre-devel

zlib库提供了不少种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,因此须要在Centos上安装zlib库。后端

yum install -y zlib zlib-devel

安装GCC和OpenSSL安全

yum install gcc-c++
yum install -y openssl openssl-devel

如今咱们开始安装nginx,这里我安装的是1.14.0版本bash

wget -c https://nginx.org/download/nginx-1.14.0.tar.gz

解压并进入nginx目录

tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0

使用nginx的默认配置

./configure

编译安装

make
make install

查找安装路径:

[root@iZ8vbfhrv1vsbp44n9fdtoZ ~]# whereis nginx
nginx: /usr/local/nginx

进入sbin目录,能够看到有一个可执行文件nginx,直接./执行就OK了。

[root@iZ8vbfhrv1vsbp44n9fdtoZ ~]# cd /usr/local/nginx
[root@iZ8vbfhrv1vsbp44n9fdtoZ nginx]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp
[root@iZ8vbfhrv1vsbp44n9fdtoZ nginx]# cd sbin
[root@iZ8vbfhrv1vsbp44n9fdtoZ sbin]# ls
nginx
[root@iZ8vbfhrv1vsbp44n9fdtoZ sbin]# ./nginx

配置开机自启动
在rc.local增长启动代码就能够。增长一行 /usr/local/nginx/sbin/nginx

vi /etc/rc.local

设置执行权限:

chmod 755 rc.local

配置

nginx配置文件修改

vim /usr/local/nginx/conf/nginx.conf

修改原配置文件的部份内容

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   /opt/nodejs/blog-server/static/blog;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    # proxy to the blog server
    location /blog {
        proxy_pass http://0.0.0.0:3000;
    }
}

这样访问80端口根路径就会请求index.html文件,匹配/blog路径的请求会被转发到本地服务器的3000端口上。
配置完毕后,能够执行命令检查一下配置是否有错误

[root@iZ8vbfhrv1vsbp44n9fdtoZ conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

最后重启一下nignx就能够了

/usr/local/nginx/sbin/nginx -s reload

此时你访问域名,就会按照nginx配置请求先后端资源。访问个人网站(http://www.readingblog.cn/#/b... 试试吧。

相关文章
相关标签/搜索