最近在看深入理解nginx这本书,看完了前两章,先记录下心得吧,来个初步的直观体验。
一 准备步奏如下:
1.当然是先搞个linux服务器啦,但是发现自己搭建比较麻烦而且家里的话估计也就只能搞个虚拟机,后来用了阿里云的ecs,0元免费体验6个月,只需要花费100多块1M宽带的money,就能半年拥有自己的云服务器,感觉还是相当的划算。大家可以去阿里云的官网体验下http://www.aliyun.com/。
2.有了服务器当然就是装个ftp功能,傻瓜式操作见之前博客。
3.安装jdk这个也不说了。
4.我是先装了两个tomcat用来调试,注意同一台机器的话,tomcat有几个端口重复的,具体错误直接看tomcat的启动日志就能看出来哪些端口冲突了。
5.下载nginx rpm包,视OS具体版本来下载rpm,查看OS版本号的命名 uname -a 或者 cat /etc/issue。
6.安装nginx相关环境
--安装gcc
yum install -y gcc
--安装正则
yum install -y pcre-devel
--安装压缩库
yum install -y zlib zlib-devel
--安装openssl
yum install -y openssl openssl-devl
--安装rpm包
rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm
--安装nginx
yum install nginx
7.安装完了当然是启动nginx啦
[root@iZ236yobc21Z ~]# find / -name nginx* /var/lib/yum/repos/x86_64/6/nginx /var/log/nginx /var/cache/yum/x86_64/6/nginx /var/cache/nginx /etc/rc.d/init.d/nginx /etc/sysconfig/nginx /etc/nginx /etc/logrotate.d/nginx /usr/sbin/nginx /usr/share/nginx /root/nginx --查看nginx版本 [root@iZ236yobc21Z share]# yum info nginx Loaded plugins: security Installed Packages Name : nginx Arch : x86_64 Version : 1.6.1 Release : 1.el6.ngx Size : 827 k Repo : installed From repo : nginx Summary : High performance web server URL : http://nginx.org/ License : 2-clause BSD-like license Description : nginx [engine x] is an HTTP and reverse proxy server, as well as : a mail proxy server. --启动nginx [root@iZ236yobc21Z share]# service nginx start Starting nginx: [ OK ]8.可以测试nginx是否已经启动成功啦
http { include /etc/nginx/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 /etc/nginx/conf.d/*.conf; server{ #监听端口 listen 80 ; server_name 121.40.103.119; location / { proxy_pass http://121.40.103.119:8080; # proxy_redirect off; # proxy_set_header X-Real-IP $remote_addr; # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }-- 校验nginx配置是否正确
/usr/sbin/nginx -t
-- 不重启nginx的情况下重新加载配置文件
service nginx reload
3.验证反向代理是否成功 ,可见nginx已经将http转发出去啦。
2.修改nginx配置文件
http { include /etc/nginx/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 /etc/nginx/conf.d/*.conf; upstream testServer{ server 121.40.103.119:8080; server 121.40.103.119:8090; } server{ listen 80 ; server_name 121.40.103.119; location / { proxy_pass http://testServer; } } }
/usr/sbin/nginx -t
-- 不重启nginx的情况下重新加载配置文件
service nginx reload
3.验证负载均衡是否成功,上面我们没有配置策略,但是默认是轮询的。
有个疑问,为何server节点都配置80节点呢,默认是跟nginx的端口保持一致的?
带着这个疑问我把端口改成了880,结果令人有点意外,后来还是想通了。
因为当通过dns服务器解析域名到该nginx服务器上的时候,nginx会解析http包,同时发现请求包里面有这个请求端口,所以就转发了该请求包,我的理解是类似修改了http请求报文。
server{ listen 880 ; server_name 121.40.103.119; location / { proxy_pass proxy_pass http://121.40.103.119:8080; } }
-- 校验nginx配置是否正确
/usr/sbin/nginx -t
-- 不重启nginx的情况下重新加载配置文件
service nginx reload