Nginx安装和反向代理配置

        Nginx安装需要一些准备工作。 安装gcc等

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

    还需要安装pcre,PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 Perl兼容的正则表达式库。

yum -y install pcre

 下载Nginx源码包,这里选择是1.7.8版本。并且解压缩,并且编译

wget http://nginx.org/download/nginx-1.7.8.tar.gz
tar zxcf nginx-1.7.8.tar.gz
./configure --prefix=/usr/local/nginx --with-pcre    //--prefix=表示安装的路径
make && make install

编译成功后,进入Nginx目录查看下版本,验证是否安装完成。

[root@ACtest ~]# cd /usr/local/nginx/sbin/
[root@ACtest sbin]#/usr/local/nginx/sbin/nginx       //启动Nginx
[root@ACtest sbin]# ./nginx -v                       //验证下Nginx版本
nginx version: nginx/1.7.8
[root@chumjtest sbin]#  ps aux|grep nginx               //查看下Nginx启动进程
root      4684  0.0  0.0 103256   840 pts/1    S+   15:14   0:00 grep nginx
root     32670  0.0  0.0  24304   700 ?        Ss   Dec23   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody   32671  0.0  0.0  26804  3824 ?        S    Dec23   0:00 nginx: worker process                            
nobody   32672  0.0  0.0  26748  3396 ?        S    Dec23   0:16 nginx: worker process

安装完成后对Nginx进行配置

[root@ACtest ~]#cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak //将主配置文件进行备份
[root@ACtest ~]#vim /usr/local/nginx/conf/nginx.conf     //这里将原来的配置文件清空,替换新的配置文件。
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
    use epoll;
    worker_connections 6000;
}
http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    '$host "$request_uri" $status'
    '"$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm application/xml;
    }

将重启下Nginx,将nginx进程Kill掉即可


配置反向代理

通过nginx访问不同的web服务器的端口

如:localhost:9080 = 10.1.1.78:9080

在配置文件加入

server {
        listen 9080;             //代理的端口
        server_name localhost;  //本机的信息
        location / {
                proxy_pass       http://10.1.1.78:9080/;      //表示代理的地址
                proxy_set_header Host   $host;
                proxy_set_header X-Real-IP      $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
#            access_log  /home/logs/bb_access.log combined;
}

配置完后,重启下Nginx。之后测试下。

[root@ACtest sbin]# curl -I http://localhost:9080
HTTP/1.1 200 OK
Server: nginx/1.7.8
Date: Mon, 26 Dec 2016 07:52:37 GMT
Content-Type: text/html
Content-Length: 1812
Connection: keep-alive
Last-Modified: Tue, 14 Jun 2016 01:53:30 GMT
ETag: "714-53533459b292a"
Accept-Ranges: bytes

测试成功,表示网站可以正常访问。