• 了解nginx的正向代理与反向代理的概念和区别
  1. 正向代理:代理客户端,客户端访问服务器,无法直接访问,需要一个代理,将客户端的地址转换成可以访问服务端的地址,然后去访问服务端,典型示例:vpn
  2. 反向代理:设置统一的服务器入口(对客户端来说),将服务器隐藏
  3. 正向代理是为客户端服务,反向代理为服务端服务,正向代理代理客户端,反向代理代理服务端(这句话是借鉴别人的)
  • 了解nginx的正向代理和反向代理的配置方法
  1. 正向代理的配置:
  2. 反向代理的配置:

正向代理与反向代理的配置是一样的。都是监控端口,将其转发到对应的IP上,进行访问。
配置如下:

vim /etc/nginx/sites-enabled/default

# You may add here your
# server {
#	...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
#ohttp://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

upstream nginx_server{
        server 192.168.1.65:80 weight=5;
        server 192.168.1.66:80 weight=5;
        server 192.168.1.67:80 weight=5;
}

server {
	listen   80 backlog=20480;  #8192; ## listen for ipv4; this line is default and implied
	#listen   [::]:80 default ipv6only=on; ## listen for ipv6

	root /var/www;
	index index.html index.htm index.php;

	# Make site accessible from http://localhost/
	server_name localhost;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to index.html
		proxy_pass http://nginx_server;
		try_files $uri $uri/ $uri/index.html;
		# Uncomment to enable naxsi on this location
		# include /etc/nginx/naxsi.rules
	}

#	location /doc/ {
#		alias /usr/share/doc/;
#		autoindex on;
#		allow 127.0.0.1;
#		#allow all;
#		deny all;
#	}

	# Only for nginx-naxsi : process denied requests
	#location /RequestDenied {
		# For example, return an error code
		#return 418;
	#}

	#error_page 400 401 402 403 404 /50x.html;

	# redirect server error pages to the static page /50x.html
	#
#	error_page 500 502 503 504 /50x.html;
#	location = /50x.html {
#		root /var/www/;
#	}

	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
	#
	location ~ \.php$ {
#		fastcgi_split_path_info ^(.+\.php)(/.+)$;
	#	# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
	#
	#	# With php5-cgi alone:
		fastcgi_pass 127.0.0.1:9000;
	#	# With php5-fpm:
	#	fastcgi_pass unix:/var/run/php5-fpm.sock;
		fastcgi_index index.php;
		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 443;
	server_name localhost;
#
 	root /var/www;
        index index.html index.htm index.php;
#
	ssl on;
	ssl_certificate cert.pem;
	ssl_certificate_key cert.key;
#
	ssl_session_timeout 5m;
#
	ssl_protocols   TLSv1  TLSv1.1 TLSv1.2;
	ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+EXP;
	ssl_prefer_server_ciphers on;
#
	location / {
		try_files $uri $uri/ $uri/index.html;
	}
location ~ \.php$ {
#               fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
                fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
        #       fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

}

增加下面这一行,即可实现反向代理,只要改掉对应的ip即可
proxy_pass http://nginx_server;

  • 了解nginx的负载均衡

nginx的负载均衡使用反向代理实现

  • keepalived与nginx组合使用原理

keepalived建立虚拟ip,搭建主从模式,实现高可用(HA),nginx部署再keepalived的主从节点上,实现了nginx的HA,nginx监控的端口是keepalived的虚拟端口,转发的则是多台nginx节点

  • keepalived与nginx组合使用配置

nginx的配置就像上面的一样,keepalived的配置则不需要配置lvs的realserver像,只要将vip放出来就可以了。