一、Nginx快速配置
1、Nginx快速安装
以 CentOS7 为例,介绍如在虚拟机快速安装并配置 Nginx-1.21
1、安装 Nginx 之前,虚拟机需要 gcc 安装环境 、 pcre 库 、zlib 库
1 2
| yum install -y gcc pcre pcre-devel zlib zlib-devel
|
2、安装Nginx,直接从官网下载Nginx的安装包
1 2 3 4 5 6
| tar zxvf nginx-1.21.tar.gz
./configure --prefix=/usr/local/nginx
make & make install
|
3、接下来就是启动Nginx了
1 2 3 4 5 6
| cd /usr/local/nginx/sbin/
./nginx
ps - ef | grep nginx
|
4、此时,还不能直接访问 localhost ,因为防火墙可能没放行呀,防火墙开放80端口
1 2 3 4
| firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
|
5、如果想要Nginx配置成开机自启动,就需要把他安装成系统服务,设置开启自启动
(1)首先创建脚本服务
1 2
| vi /usr/lib/systemd/system/nginx.service
|
(2)脚本的内容为(注意里边的文件路径要写自己的的呀)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [Unit] Description=nginx - web server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop ExecQuit=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
|
(3)重启加载系统服务
(4)启动服务
1
| systemctl start nginx.service
|
(5)配置开机自启动
1
| systemctl enable nginx.service
|
这里再提供一种在线安装的方式,更加简单快捷,默认配置文件在 /etc/nginx/conf.d/default.conf 下
1 2 3 4 5 6
| rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum -y install nginx
systemctl enable nginx.service
|
2、Nginx快速配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| worker_processes 1; events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
|
3、安装Keepalived
Keepalived是一个基于VRRP协议来实现的服务高可用方案,可以利用其来避免IP单点故障,类似的工具还有heartbeat、corosync、pacemaker。但是它一般不会单独出现,而是与其它负载均衡技术(如lvs、haproxy、nginx)一起工作来达到集群的高可用。这里展示一下 Keepalived 的快速配置
1、安装 Keepalived 所需要的环境依赖
1
| yum -y install openssl-devel
|
2、安装keepalived
1
| yum -y install keepalived
|
3、进入配置文件,配置相应端口
1
| /etc/keepalived/keepalived.conf
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| ! Configuration File for keepalived
global_defs { router_id group1 }
vrrp_instance baixxq { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.112.99 } }
|
4、启动服务
1
| systemctl start keepalived
|
5、配置开机自启
1
| systemctl enable keepalived.service
|
二、Nginx初级入门
1、Nginx目录结构
1 2 3 4 5 6 7 8
| client_body_temp logs proxy_temp uwsgi_temp fastcgi_temp scgi_temp conf html sbin
|
2、Nginx配置文件详解
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
|
worker_processes 1;
events { worker_connections 1024; }
http {
include mime.types; default_type application/octet-stream; sendfile on;
upstream mysvr { server 192.168.80.121:80 weight=3; server 192.168.80.122:80 weight=2; server 192.168.80.123:80 weight=3; } server { //listen 127.0.0.1:8000; //listen 127.0.0.1; //listen 8000; //listen *:8000; //listen localhost:8000; listen 80;
server_name 127.0.0.1; location ~*^.+$ { proxy_pass http://mysvr; //请求转向mysvr 定义的服务器列表 } } }
|
参考链接 : Nginx配置文件详解
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
| //########### 每个指令必须有分号结束。################# //配置用户或者组,默认为user nobody nobody; 将user指令注释掉,或者配置成nobody的话所有用户都可以运行
//指定工作线程数,可以制定具体的进程数,也可使用自动模式 ,worker_processes number | auto;这种情况下会生成一个master进程和number个worker进程 worker_processes 1;
// error_log [path] [debug | info | notice | warn | error | crit | alert | emerg]
events { //当某一时刻只有一个网络连接到来时,多个睡眠进程会被同时叫醒,但只有一个进程可获得连接。如果每次唤醒的进程数目太多,会影响一部分系统性能。在Nginx服务器的多进程下,就有可能出现这样的问题。 worker_connections 1024; }
//HTTP块 http { //常用的浏览器中,可以显示的内容有HTML、XML、GIF及Flash等种类繁多的文本、媒体等资源,浏览器为区分这些资源,需要使用MIME Type。换言之,MIME Type是网络资源的媒体类型。Nginx服务器作为Web服务器,必须能够识别前端请求的资源类型。你可以加一个mp9 ,指定解析格式 include mime.types; //#文件扩展名与文件类型映射表,mime.types和ngin.cfg同级目录,不同级的话需要指定具体路径 default_type application/octet-stream; //#默认文件类型,如果不加此指令,默认值为text/plain。 //在全局块中,我们介绍过errer_log指令,其用于配置Nginx进程运行时的日志存放和级别,此处所指的日志与常规的不同,它是指记录Nginx服务器提供服务过程应答前端请求的日志
sendfile on; // //# 下面配置的含义是,在服务器端保持连接的时间设置为120 s,发给用户端的应答报文头部中Keep-Alive域的超时时间设置为100 s。header_timeout,可选项,在应答报文头部的Keep-Alive域设置超时时间: //# keepalive_timeout 120s 100s //Nginx服务器端和用户端建立会话连接后,用户端通过此连接发送请求。指令keepalive_requests用于限制用户通过某一连接向Nginx服务器发送请求的次数。默认是100
//负载均衡配置 upstream mysvr { server 192.168.80.121:80 weight=3; server 192.168.80.122:80 weight=2; server 192.168.80.123:80 weight=3; } server { //listen 127.0.0.1:8000; //listen 127.0.0.1; //listen 8000; //listen *:8000; //listen localhost:8000; listen 80; //#监听端口
server_name 127.0.0.1; //#监听地址 www.lovebai.fun location ~*^.+$ { proxy_pass http://mysvr; //请求转向mysvr 定义的服务器列表 } } }
|
例子
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
user www www;
worker_processes 8;
error_log /usr/local/nginx/logs/error.log info;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 65535;
events { use epoll;
worker_connections 65535;
keepalive_timeout 60;
client_header_buffer_size 4k;
open_file_cache max=65535 inactive=60s;
open_file_cache_valid 80s;
open_file_cache_min_uses 1; open_file_cache_errors on; }
http { include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 64k;
client_max_body_size 8m;
sendfile on;
autoindex on;
tcp_nopush on; tcp_nodelay on;
keepalive_timeout 120;
fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k;
gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on;
upstream jh.w3cschool.cn { server 192.168.80.121:80 weight=3; server 192.168.80.122:80 weight=2; server 192.168.80.123:80 weight=3;
} server { listen 80;
server_name www.w3cschool.cn w3cschool.cn; index index.html index.htm index.php; root /data/www/w3cschool;
location ~ .*.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { expires 10d; } location ~ .*.(js|css)?$ { expires 1h; } log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; access_log /usr/local/nginx/logs/host.access.log main; access_log /usr/local/nginx/logs/host.access.404.log log404; location / { proxy_pass http://127.0.0.1:88; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_intercept_errors on;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k; } location /NginxStatus { stub_status on; access_log on; auth_basic "NginxStatus"; auth_basic_user_file confpasswd; } location ~ .(jsp|jspx|do)?$ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8080; } location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt| pdf|xls|mp3|wma)$ { expires 15d; } location ~ .*.(js|css)?$ { expires 1h; } } }
|