Linux下 安装Ngnix方法及配置

1.Ngnix下载安装

  • 安装相关依赖

    yum -y install gcc zlib-devel pcre-devel openssl openssl-devel

  • 使用wget 下载 ,若没安装(yum install wget)

cd /usr/local
mkdir nginx
cd nginx
wget http://nginx.org/download/nginx-1.16.1.tar.gz
//解压

tar -zxvf nginx-1.16.1.tar.gz

//指定安装位置

./configure –prefix=/usr/local/nginx

//编译与安装

make && make install

2.Ngnix 目录结构

–conf /nginx.conf nginx配置文件

–html 存放静态文件(html,css,js等)

–logs 日志目录 存放日志文件

–sbin 二进制文件 用于启动停止nginx服务

├── conf
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── html
│ ├── 50x.html
│ └── index.html
├── logs
└── sbin
└── nginx

2.Ngnix 常用命令

查看当前nginx版本

在nginx/sbin下执行

./nginx -v

查看当前nginx配置文件是否正常

./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

启动nginx服务

./nginx

停止nginx服务

./nginx -s stop

查看nginx服务

ps -ef |grep nginx

重新加载nginx配置文件

./nginx -s reload

访问:192.168.134.128 注意防火墙是否开启

1.全局配置 nginx运行相关的全局配置

2.events块 和网络连接相关的配置

3.http块 代理、缓存、日志记录、虚拟主机配置

http全局块

Server块

        server全局块

        location块

配置文件示例:

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

#user nobody;worker_processes 1;

#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;

#pid logs/nginx.pid;

events { worker_connections 1024;}

http {

include mime.types;

default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80; #默认监听端口
server_name localhost; #服务器名称

#charset koi8-r;

#access_log logs/host.access.log main;

location / { #匹配客户端请求url
root html; #指定静态资源根目录
index index.html index.htm; #指定默认首页
proxy_pass http://192.168.134.127:8080;#反向代理配置,将请求转发到指定服务
}

#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 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;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}
}

反向代理配置示例:

1
2
3
4
5
6
7
8
location ^~ /api/{

rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://192.168.134.128:8080;

}

# 将请求路径重写成 如 /api/user/getList (.*)截取匹配 /user/getList -> http://192.168.134.128:8080/user/getList

rewrite 重定向:需要先做正则匹配,然后再给客户端返回新地址进行重定向

常用匹配符号解释:

=:表示精确匹配,优先级最高,匹配成功后则停止向下搜索。

^~:对uri 起始字符 做 _字符串匹配_,不是 _正则匹配_。 区分大小写

upstream targetserver{ #upstream 指令可以定义一组服务器

            server 192.168.134.127:8080 weigth=10; #weight 权值大 分发机率越大

            server 192.168.134.127:8081 weigth=5;

}

负载均衡策略

名称 说明
轮询 默认方式
weight 权重方式
ip_hash 根据ip分配方式
least_conn 依据最少连接方式
url_hash 依据url 分配方式
fair 依据响应时间方式