nginx的配置及以反向代理方式实现动静分离、负载均衡

整个安装环境用root安装,本文中所有环境的配置以及安装均在/usr/local目录下,如需要在其他目录下安装根据需要进行调整

配置安装环境:

1.解压缩pcre-8.20.tar.gz 并重命名为pcre,进入pcre文件夹
./configure
make
make install


2.安装zlib和openssl

cd ../   #退出pcre安装目录
yum install zlib*
解压缩openssl-1.0.0d.tar.gz 并且重命名为openssl,进入openssl文件夹
./config --prefix=/usr/local/openssl
make
make install


3.解压缩yamdi-1.4.tar.gz并且重命名为yamdi(拖动模块)进入yamdi文件夹
make
make install


以上环境配置完成,可以进行nginx的安装。

解压缩nginx的tar包,并重命名为nginx,进入nginx文件夹

./configure --user=root --group=root --prefix=/usr/local/nginx --with-http_realip_module  --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre --with-openssl=/usr/local/openssl --with-http_sub_module --with-md5=/usr/local/lib --with-sha1=/usr/local/lib --with-http_gzip_static_module --with-http_flv_module --with-cc=gcc --with-cc-opt='-O3'

这边要特别注意路径,路径写错后面是无法解析安装的。出现这个界面后,就可以进行下一步安装了(截图中的安装路径来自/ap/nginx目录,所以大家出现的界面路径会略有不同)


make

这里时间有点长,可以来一杯20年陈酿雪碧静静等待。

make install

安装完成后,ls一下,看是否出现sbin、conf等目录,如果出现就说明安装成功。


如果安装过程中出现小的错误,后面启动的时候缺什么补什么就行了,笔者安装完成之后缺少logs日志文件目录,启动失败,于是自己在nginx目录下

mkdir logs

cd logs

touch error.log

就OK了,但是如果安装失败,少很多东西,建议删除后重新安装

安装好之后进入conf文件夹进行动静分离的配置,进入之后先把原装的进行备份,建立新的配置文件

mv nginx.conf nginx.conf.bak

vim nginx.conf

下面就是配置反向代理的过程了,将下面的额配置代码加进去即可


worker_processes  2;  #这个数字跟你Linux服务器的CPU核数保持一致,默认为1


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


pid        logs/nginx.pid;




events {
    worker_connections  1024;
}




http {
    include       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  logs/access.log  main;  




    #设定请求缓冲  
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 300m;
    sendfile on;
    tcp_nopush     on;
    keepalive_timeout 60;
    tcp_nodelay on;
    server_tokens off;
    client_body_buffer_size 512k;
    proxy_connect_timeout   5;
    proxy_send_timeout      60;
    proxy_read_timeout      5;
    proxy_buffer_size       16k;
    proxy_buffers           4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    client_header_timeout  3m;
    client_body_timeout    3m;
    send_timeout           3m;




    gzip on; #开启gzip,节省带宽  
    gzip_min_length  1100;
    gzip_buffers     4 8k;
    gzip_types       text/plain text/css application/x-javascript image/bmp application/javascript;


    output_buffers   1 32k;
    postpone_output  1460;


    limit_rate_after 3m;     #限速模块,前3M下载时不限速  
    limit_rate 512k;          #限速模块   




    server {
        listen       80;   #监听80 端口
        server_name  127.0.0.1;   #本机


        #charset koi8-r;  


        access_log  logs/host.access.log;




        #自动补全"/"  
        if (-d $request_filename){
         rewrite ^/(.*)([^/])$ http://$host/$1$2/ last;
        }


        #location ~ \.(jsp|page|do)?$ {    #动态访问转向tomcat处理
        #   proxy_set_header  Host $host;  
        #   proxy_set_header  X-Real-IP  $remote_addr;  
        #   proxy_pass http://127.0.0.1:8080;   
        #}  


        location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$  {  #设定访问静态文件直接读取不经过tomcat
           root /usr/local/nginx/html;  #将工程放入到这个目录下,静态的图片等资源直接从该目录下读出
        }
        #location ~ .*\.(js|css)?$ {  
        #   expires      1h;  #设置缓存时间
        #}     


        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass  http://127.0.0.1:8080;
        }


        error_page  404              /404.html;
        location = /40x.html {
            root   html;
        }



        # redirect server error pages to the static page /50x.html  
        #  


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }



        # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
        #  
        #location ~ \.php$ {  
        #    proxy_pass   http://127.0.0.1;  
        #}  


        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
        #  
        #location ~ \.php$ {  
        #    root           html;  
        #    fastcgi_pass   127.0.0.1:9000;  
        #    fastcgi_index  index.php;  
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
        #    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       8000;  
    #    listen       somename:8080;  
    #    server_name  somename  alias  another.alias;  


    #    location / {  
    #        root   html;  
    #        index  index.html index.htm;  
    #    }  
    #}  



  #  upstream mcul {    #########配置负载均衡
  #     server 127.0.0.1:8080 weight=1;    
  #    server 127.0.0.1:9080 weight=2;    
   # }    




    # HTTPS server  
    #  
    #server {  
    #    listen       443;  
    #    server_name  localhost;  


    #    ssl                  on;  
    #    ssl_certificate      cert.pem;  
    #    ssl_certificate_key  cert.key;  


    #    ssl_session_timeout  5m;  


    #    ssl_protocols  SSLv2 SSLv3 TLSv1;  
    #    ssl_ciphers  HIGH:!aNULL:!MD5;  
    #    ssl_prefer_server_ciphers   on;  


    #    location / {  
    #        root   html;  
    #        index  index.html index.htm;  
    #    }  
    #}  


}



配置完成后wq保存退出,进入sbin目录下

启动nginx:./nginx

重启nginx:./nginx -s reload

停止nginx:./nginx -s stop
 启动后测试的时候在地址栏里可以省略掉端口号直接进行连接访问了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值