# 备份原配置文件 $ cd /usr/local/nginx $ mv conf/nginx.conf conf/nginx.conf.bak # 修改配置 $ vim /usr/local/nginx/conf/nginx.conf user ?nginx; worker_processes ?5; error_log ?/var/log/nginx/error.log warn; pid ? ? ? ?/var/run/nginx.pid; events { ? ?worker_connections ?1024; } http { ? ?include ? ? ? /usr/local/nginx/conf/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 ?/var/log/nginx/access.log ?main; ? ?sendfile ? ? ? ?on; ? ?#tcp_nopush ? ? on; ? ?keepalive_timeout ?65; ? ?#gzip ?on; ? ?include /usr/local/nginx/conf/conf.d/*.conf; }
创建站点配置文件
$ vim /usr/local/nginx/conf/conf.d/site.conf upstream test { ?# fake server otherwise ngx_http_upstream will report error when startup ?server 127.0.0.1:22222; ?# all backend server will pull from consul when startup and will delete fake server ?# 后端使用consul存储 ?# upsync 192.168.2.210:8500/v1/kv/upstreams/test upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off; ?# 后端使用etcd存储 ?upsync 192.168.2.210:2379/v2/keys/upstreams/test upsync_timeout=6m ?upsync_interval=500ms upsync_type=etcd strong_dependency=off; ?upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf; ?#配置健康检查 ?check interval=1000 rise=2 fall=2 timeout=3000 type=http default_down=false; ?check_http_send “HEAD / HTTP/1.0\r\n\r\n”; ?check_http_expect_alive http_2xx http_3xx; } upstream bar { ?server 192.168.2.210:8080 weight=1 fail_timeout=10 max_fails=3; } server { ?listen 80; ?location = / { ? ?proxy_pass http://test; ?} ?location = /bar { ? ? ?proxy_pass http://bar; ?} ?location = /upstream_show { ? ?upstream_show; ?} ?location = /upstream_status { ? ?check_status; ? ?access_log off; ?} }
$ vi /lib/systemd/system/nginx.service [Unit] Description=The NGINX HTTP and reverse proxy server Documentation=http://nginx.org/en/docs/ After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/var/run/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=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
修改权限及加入开机启动
$ sudo chmod +x /lib/systemd/system/nginx.service
$ sudo systemctl enable nginx.service
现在可以使用下面的指令来管理Nginx服务.
$ systemctl start nginx.service
$ systemctl reload nginx.service
$ systemctl restart nginx.service
$ systemctl stop nginx.service
$ systemctl start nginx.service
验证Nginx服务是否正常
$ systemctl status ?nginx.service ● nginx.service – The NGINX HTTP and reverse proxy server ? Loaded: loaded (/lib/systemd/system/nginx.service; disabled; vendor preset: enabled) ? Active: active (running) since Mon 2017-05-08 10:58:06 CST; 1min 6s ago ? ? Docs: http://nginx.org/en/docs/ ?Process: 22966 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited,status=0/SUCCESS) ?Process: 22963 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=exited,status=0/SUCCESS) Main PID: 22971 (nginx) ? ?Tasks: 6 ? Memory: 24.5M ? ? ?CPU: 517ms ? CGroup: /system.slice/nginx.service ? ? ? ? ? ├─22971 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.con ? ? ? ? ? ├─22972 nginx: worker process ? ? ? ? ? ├─22973 nginx: worker process ? ? ? ? ? ├─22974 nginx: worker process ? ? ? ? ? ├─22975 nginx: worker process ? ? ? ? ? └─22976 nginx: worker process May 08 10:58:06 dev-master-01 systemd[1]: Starting The NGINX HTTP and reverse proxy server… May 08 10:58:06 dev-master-01 nginx[22963]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok May 08 10:58:06 dev-master-01 nginx[22963]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful May 08 10:58:06 dev-master-01 systemd[1]: Started The NGINX HTTP and reverse proxy server.
如果要单独查看Nginx服务日志,可以使用以下命令:
$ journalctl -f -u nginx.service
验证Upsync模块
启动一个Web服务
这里在Nginx默认站点目录,用Python启动一个SimpleHTTPServer进行验证.
$ cd /usr/local/nginx/html
$ python -m SimpleHTTPServer 8080
后端存储增加数据
增加一个后端服务器
$ curl -X PUT http://192.168.2.210:2379/v2/keys/upstreams/test/192.168.2.210:8080
其它Upstream模块中其它属性的默认值为:weight=1 max_fails=2 fail_timeout=10 down=0 backup=0; .如果你要调整这些值,可以用以下命令格式进行提交:
$ curl -X PUT -d value=”{\&;weight\&;:1,\&;max_fails\&;:2,\&;fail_timeout\&;:10}” http://
$etcd_ip:$port/v2/keys/$dir1/$upstream_name/$backend_ip:$backend_port
删除一个后端服务器
$ curl -X DELETE http://192.168.2.210:2379/v2/keys/upstreams/test/192.168.2.210:8080
增加一个后端服务器
$ curl -X PUT http://192.168.2.210:8500/v1/kv/upstreams/test/192.168.2.210:8080
删除一个后端服务器
$ curl -X DELETE http://192.168.2.210:8500/v1/kv/upstreams/test/192.168.2.210:8080
测试站点是否正常访问
页面自动根据proxy_pass http://test; 成功转到了后端服务器.
(编辑:ASP站长网)
|