CentOS 6.5 64位下安装nginx,配置反向代理

一、环境

操作系统:CentOS 6.5 64位

软件环境:nginx

安装方式:使用yum安装

开放防火墙

SELINUX关闭


二、安装、配置NGINX

1、更新软件

[root@Init ~]# yum update -y

更新完成之后,重启操作系统

[root@Init ~]# init 6

2、安装NGINX

[root@Init ~]# yum install nginx -y

3、配置NGINX(在我的这个环境中,配置文件如下)


#user  nobody;
#worker_processes  5;
worker_processes 4;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#error_log /dev/null;

#pid        logs/nginx.pid;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;

events {
    use epoll;
    worker_connections  51200;
}

#include tcp.conf;

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 32k;
    client_max_body_size 8m;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
    log_format main  '$remote_addr - $remote_user [$time_local] $request '
        '"$status" $body_bytes_sent "$http_referer" '                                
        '"$http_user_agent" "$http_x_forwarded_for"'                
        '"$server_addr;" "$upstream_addr "';

    #access_log  logs/access.log  main;    
    access_log /dev/null;

    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay on;
    server_tokens off;

    #keepalive_timeout  0;
    keepalive_timeout  3600s;

    gzip  on;

    

    upstream api{
        #train api
        server 10.117.188.44:8001 max_fails=3;
        server 10.51.19.21:8001 max_fails=3;
    }

    server {        
        listen         80;        
        server_name    api.Init.com;        
        location / {
            proxy_next_upstream error timeout http_500 http_502 http_504;
        
            proxy_redirect        off;            
            proxy_set_header    Host    $host:80;            
            proxy_set_header    X-Real-IP    $remote_addr;            
            proxy_set_header    X-Forwarded-For    $proxy_add_x_forwarded_for;             
            #proxy_connect_timeout    90s;            
            proxy_send_timeout    180s;            
            proxy_read_timeout    180s;            
            #proxy_buffers        32 4K;                    
            proxy_pass    http://api;    
        }                                 
    }

    ##########################vhost#####################################
    #include vhost/*.conf;
}

4、启动NGINX服务


[root@Init nginx]# service nginx status


三、配置防火墙


######################################
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
#####################################

把文本框内容写入到/etc/sysconfig/iptables,覆盖原来的内容(如果有的话)。

[root@Init nginx]# service iptables start    #启动防火墙

[root@Init nginx]# iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT  #开放nginx的80端口

[root@Init nginx]# service iptables save  #保存防火墙的配置

四、设置开机启动

[root@Init nginx]# chkconfig nginx on   #配置nginx服务开机自起
[root@Init nginx]# chkconfig iptables on  #配置iptables服务开机自起

五、测试反向代理是否生效

在两台后端真实服务器上配置不同的页面,然后打开nginx所在服务器的地址http://IP/



posted @ 2016-06-07 20:05  刘星湘  阅读(581)  评论(0编辑  收藏  举报