码迷,mamicode.com
首页 > 其他好文 > 详细

CentOS 7 安装Nginx做反向代理

时间:2017-06-29 22:29:17      阅读:861      评论:0      收藏:0      [点我收藏+]

标签:ges   负载均衡   pretty   默认   rem   防火墙规则   type   print   post   

题记

须要使用nginx的反向代理功能,測试环境为centos+NGINX 1.8.0.

跳过一些繁琐的问题,直接记录核心

步骤

(1)centos 安装在VM中。因此须要注意网络连接问题
(2)安装nginx使用的是具有网络的yum功能
(3)配置centos防火墙,须要开启80 port
(4)nginx 反向代理配置
(5)性能优化设置(兴许工作...)

实现

一. yum安装nginx
先加入nginx源,測试使用最新的nginx 1.8.0

rpm -ivh  http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 

运行命令:

yum install nginx
service nginx start

假设不出意外。在浏览器输入 127.0.0.1:80,就可以看到nginx的欢迎界面。

二.查看nginx的配置

rpm -ql nginx
此命令能够查看nginx的配置信息

三. 关闭firewall并配置iptables

centos默认使用firewall配置port和网络,可是如今网上资料多是使用iptables。鉴于资料充分的原因,改用iptalbes。

使用iptables和ip6tables的静态防火墙规则
假设你想使用自己的 iptables 和 ip6tables 静态防火墙规则, 那么请安装 iptables-services 并且禁用 firewalld ,启用 iptables 和ip6tables:

yum install iptables-services
systemctl mask firewalld.service
systemctl enable iptables.service
systemctl enable ip6tables.service

启用iptables后,就须要进行port和訪问规则的设置了。

(1)编辑  /etc/sysconfig/iptables
(2)清空规则
(3)加入须要的规则

演示样例:

# 同意已建立的或相关连的通行
-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
#同意本地回环接口
-A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
#同意本机对外訪问
-A OUTPUT -j ACCEPT
# 同意訪问SSHport,假设port改动了能够更改对应port号
-A INPUT -p tcp –dport 22 -j ACCEPT
#同意訪问80(HTTP)port
-A INPUT -p tcp –dport 80 -j ACCEPT
#同意訪问FTPport:21、20
-A INPUT -p tcp –dport 21 -j ACCEPT
-A INPUT -p tcp –dport 20 -j ACCEPT
#同意訪问161(SNMP)port:
-A INPUT -p udp –dport 161 -j ACCEPT


基于以上配置,在局域网内能够相互訪问站点。

四. 配置nginx的反向代理功能

本次仅仅是使用反向代理功能。因此nginx的负载均衡功能就不涉及。

反向代理功能使用的是proxy_pass和sub_filter模块

location / {
    proxy_pass  须要代理的IP;

    #Proxy Settings
    proxy_redirect     off;
    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_max_temp_file_size 0;
    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;
# 做反向代理时候,出现ip地址直接跳转,没有是使用代理IP 。是由于须要使用sub_filter.
sub_filter 须要代理的IP  nginx的本机server;
sub_filter_once off;    
   }

总结:

nginx反向代理概念相对简单,并且配置也方便,接下来要进行一下压力測试。看看实际的效果。


[1]http://www.centoscn.com/CentOS/Intermediate/2015/0313/4879.html 使用iptables

[2]http://www.centoscn.com/CentOS/2013/0413/293.html 配置iptablesport和规则

[3]http://www.nginx.cn/927.html 反向代理

[4]http://zhaochen.blog.51cto.com/2029597/379233/

[5]https://github.com/yaoweibin/ngx_http_substitutions_filter_module

[6]http://www.xxorg.com/archives/3608

CentOS 7 安装Nginx做反向代理

标签:ges   负载均衡   pretty   默认   rem   防火墙规则   type   print   post   

原文地址:http://www.cnblogs.com/gavanwanggw/p/7096381.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!