阿里云服务器(1、nginx配置实战)

简介

先来一波福利广告(已过期)

双十一了,阿里云推出了史上最优惠的云服务器产品(点击这里查看详情),1核2G新用户只需要99元/年,一次性买3年只需要不到300元,价格确实极大的优惠。

要是老用户的话可以用新开一个账号,用亲戚朋友的身份证认证一下也能享受1折优惠,加入我的战队(点击这里查看详),邀请一个人差不多能瓜分战队的 50元分红+50元现金红包+25%返现 。打个比方,假如你是新用户,你买了一个3年的云服务器一共300块,买过后,你成功邀请一人(下单3年的云服务器)就可分50(战队红包) + 50(现金红包) + 300*25%(返利红包)。相当于买服务器的钱回来了一半(折上5折),听着是不是很诱惑人,进来了解下吧;目前战队排名是top15,后名次靠前的话还有更大的优惠。https://m.aliyun.com/act/team1111/#/share?params=N.QJPTsZki4i.0vr9aipb

nginx配置

连接服务器

很多前端的小伙伴买了云服务器后,担心不会配置,毕竟很多的前端还不是很会配置nginx,这里小编详细讲解下我买的阿里云服务器配置的过程。

点击上面链接就可参团购买,购买过程中推荐系统选择CentOSUbuntu系统。Ubuntu系统用户占用率高,所以文档自然也多,比较适合新手;CentOS比较适合企业和商用,一般看你们公司用的都是CentOS系统的;具体2者的区别可查看这里;小编买的是Ubuntu 16.04 64位。其他的选项选择默认就行,地区只要是国内的都很快。

买好后,点击头像=>选择产品服务=>云服务器,即可看到购买过的服务器产品。基本信息,配置信息,对cpu的监控一目了然。买阿里云的一个主要原因还是阿里云盾安全。

可以在更多里面修改远程连接密码和重置服务器密码,修改好后,尝试远程连接下,先输入远程连接密码,后登陆用户名(root)、密码即可。

当然,也可直接使用命令行链接 :

1
2
sudo ssh 服务器外网ip
输入电脑密码,输入服务器密码

除此之外,你也可用工具链接,对于 lunix 命令不是很熟悉的小伙伴比较实用。 这里推荐用TransmitFilezilla其他的都一样,用户名输入root,端口选择 22 即可。

安装Nginx

更新ubuntu软件源

1
2
3
4
sudo apt-get update
sudo apt-get install -y python-software-properties software-properties-common
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update

安装nginx

1
2
3
sudo apt-get install nginx
# 检查是否安装成功
nginx -v

安装好的文件位置:

1
2
3
4
/usr/sbin/nginx:主程序
/etc/nginx:存放配置文件
/usr/share/nginx:存放静态文件
/var/log/nginx:存放日志

其实从上面的根目录文件夹可以知道,Linux系统的配置文件一般放在 /etc,日志一般放在 /var/log,运行的程序一般放在 /usr/sbin 或者 /usr/bin。当然,如果要更清楚Nginx的配置项放在什么地方,可以打开 /etc/nginx/nginx.conf

安装其他常用工具

1、安装nodejs

1
2
3
sudo apt-get install nodejs
sudo apt install nodejs-legacy
sudo apt install npm

更新npm的包镜像源,方便快速下载

1
2
sudo npm config set registry https://registry.npm.taobao.org
sudo npm config list

全局安装n管理器(用于管理nodejs版本)

1
sudo npm install n -g

安装最新的nodejs(stable版本)

1
2
3
4
sudo n stable
# 检查是否安装成功
node -v
npm -v

2、安装webpack

1
2
npm install webpack-cli -g
npm install webpack -g

3、安装git

如果系统未安装git的话,就会非常友好的提示安装git的命令:

1
apt-get install git

在云服务器上,关于git的相关操作就不方便用图形界面化工具了,现在命令行的优势出来了,具体git命令请参考

遗漏的工具后续会补上

配置nginx

nginx安装完成后,配置文件为 /etc/nginx/nginx.conf/etc/nginx/conf.d/default.conf

1
2
3
4
cd /etc/nginx
vim nginx.conf
cd /etc/nginx/conf.d
vim default.conf

自定义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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#运行用户,默认即是nginx,可以不进行设置
user root;
#Nginx进程,一般设置为和CPU核数一样
worker_processes 1;
#进程pid存放位置
pid /run/nginx.pid;

events {
worker_connections 1024; # 单个后台进程的最大并发数
# multi_accept on;
}

http {
##
# Basic Settings
##
sendfile on; #开启高效传输模式
tcp_nopush on; #减少网络报文段的数量
tcp_nodelay on;
keepalive_timeout 65; #保持连接的时间,也叫超时时间
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types; #文件扩展名与类型映射表
default_type application/octet-stream; #默认文件类型

##
# SSL Settings
##

ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on; #开启gzip压缩
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf; #包含的子配置项位置和文件
# include /etc/nginx/sites-enabled/*;
}

#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}

自定义default.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
server {
listen 80; #配置监听端口
server_name localhost; #配置域名
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;

location / {
#服务默认启动目录
root /usr/share/nginx/html/pc; #pc
# nginx适配pc和app设备
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
root /usr/share/nginx/html/app; #app
}
index index.html; #默认访问文件
allow all; #允许访问的ip
# deny all; #拒绝访问的ip
}

# redirect error pages to the static page /404.html
error_page 404 /static/html/404/404.html; # 配置404页面
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /static/html/404/500.html; #错误状态码的显示页面,配置后需要重启

# ^~ 表示uri以某个常规字符串开头,大多情况下用来匹配url路径,nginx不对url做编码,因此请求为/static/20%/aa,
# 可以被规则^~ /static/ /aa匹配到(注意是空格)。
location ^~ /static/ {
root /usr/share/nginx/html;
allow all; #允许访问的ip
# deny all; #拒绝访问的ip
}

location = /50x.html {
root /usr/share/nginx/html/pc/; #pc
# nginx适配pc和app设备
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
root /usr/share/nginx/html/app/; #app
}
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

自定义默认目录

我们服务的默认目录放在 /usr/share/nginx/html 了下。

1
2
cd /usr/share/nginx/html
ls

假如你要定义服务器的默认访问目录,修改 location / 中的root即可,不过需要开通下你自定义目录的权限。

/root/www是自定义目录为例

1
2
3
# 需要一层层分别开通权限
chmod -R 777 /root
chmod -R 777 /root/www

假如你使用的是 Transmit 等服务器工具,也可用工具查看

自定义错误页面

1
2
3
4
# redirect  error pages to the static page /404.html
error_page 404 /static/html/404/404.html; # 配置404页面
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /static/html/404/500.html; #错误状态码的显示页面,配置后需要重启

然后在你默认或者自定义的服务器目录下,根据你的错误页配置,新建相关的页面即可。

Nginx访问权限和路径匹配规则

在匹配规则里面,有2个字段可以控制这个规则下的访问权限

1
2
3
4
location / {
allow all; #允许访问的ip
# deny all; #拒绝访问的ip
}

实际情况中,访问权限的控制还是比较复杂的,例如,要求服务器 static(静态目录)所有用户都能访问,且,重新定义访问路径,我们需要location块来完成相关的需求匹配。

1
2
3
4
5
6
7
# ^~ 表示uri以某个常规字符串开头,大多情况下用来匹配url路径,nginx不对url做编码,因此请求为/static/20%/aa,
# 可以被规则^~ /static/ /aa匹配到(注意是空格)。
location ^~ /static/ {
root /usr/share/nginx/html;
allow all; #允许访问的ip
# deny all; #拒绝访问的ip
}

对于nginx路径匹配规则,也需要简单的了解一下

  • = 表示精确匹配
  • ^~ 表示uri以某个常规字符串开头,大多情况下用来匹配url路径,nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。
  • ~ 正则匹配(区分大小写)
  • ~* 正则匹配(不区分大小写)
  • !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
  • / 任何请求都会匹配

符号的优先级

首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。

Nginx反向代理的设置

后续补充。

Nginx适配PC或移动设备

如上述配置:

1
2
3
4
5
6
#服务默认启动目录
root /usr/share/nginx/html/pc; #pc
# nginx适配pc和app设备
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
root /usr/share/nginx/html/app; #app
}
1
2
3
4
5
6
#服务默认启动目录
root /usr/share/nginx/html/pc; #pc
# nginx适配pc和app设备
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
root /usr/share/nginx/html/app; #app
}

可用app和pc访问我的服务器查看效果 47.99.212.100

配置阿里云安全组

从上面的配置文件可以看出,nginx服务监听的是80端口,所以记得到ECS实例一下打开端口。步骤如下:

  • 进入阿里云控制台,并找到ECS实例。
  • 点击实例后边的”更多”
  • 点击”网络和安全组” ,再点击”安全组配置”
  • 选择”安全组列表”,再点击”安全组配置”,再点击”加入安全组规则”
  • 进行80端口的设置,具体设置如图。

nginx 相关命令

在nginx配置的过程中,我们①先安装nginx;安装好后,检查是否安装成功;②然后开始做相关nginx配置③配置完后检查配置是否正常④然后启动nginx⑤后续每次改动nginx配置都需重启nginx

启动nginx 服务

1
2
3
4
#方法一
nginx
#方法二
systemctl start nginx.service

查看所有启动的nginx进程

1
ps aux | grep nginx

停止Nginx服务

1
2
3
4
5
6
#方法一
nginx -s stop
#方法二
nginx -s quit
#方法三
killall nginx

检查nginx配置是否正常

1
nginx -t

重启nginx服务

1
sudo nginx -s reload

查看端口占用情况

1
netstat -tlnp

nginx操作常用的一些命令

ssh链接云服务器

1
sudo ssh 服务器外网ip

新建文件夹

1
mkdir www

移动文件

1
mv node-v6.10.0-linux-x64 nodejs

解压文件

1
tar -xvf node-v6.10.0-linux-x64.tar.xz

查看文件路径

1
pwd

访问文件

1
2
cd
ls

编辑文件

1
vim nginx.conf

0%