Linux 下Nginx 的安装及负载均衡的简单配置

Posted 挖坑大王

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux 下Nginx 的安装及负载均衡的简单配置相关的知识,希望对你有一定的参考价值。

这次发布程序需要均衡负载,网上看了一下这方便的东西,觉得很不错,学完之后做下总结,一遍后期用到。

1、安装nginx之前需要安装的两个依赖,pcre-x.x.x.tar.gz 和pcre-devel-x.x.x.rpm这两个包(具体这两个有什么用处也没仔细研究过,不安装确实再安装nginx时失败)

1.1安装1.安装pcre-x.x.x.tar 

tar zxvf pcre-x.x.x.tar.gz   
cd pcre-x.x.x  
./configure  
make && make install  

1.2.安装pcre-devel-x.x.x.rpm

rpm -ivh pcre-devel-x.x.x.rpm  

2.安装nginx-x.x.x.tar.gz

tar zxvf nginx-x.x.x.tar.gz  
cd nginx-x.x.x  
./configure --with-http_stub_status_module --prefix=/usr/local/nginx --with-debug  --with-http_sub_module   
make && make install  

3.安装完成后修改配置文件

vim /usr/local/nginx/conf/nginx.conf  

修改后的配置文件如下

#user  nobody;
user root;
worker_processes  4;

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

#pid        logs/nginx.pid;

events {
    worker_connections  102400;
}

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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

   upstream Servers {
       
         server   ip:port weight=10;
         server   ip:port weight=10;
     
    }

        server {
        listen       80;
        server_name   server  www.btrcrm.com ;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #error_page  404              /404.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 Apaches document root
        # concurs with nginxs one
        #
        #location ~ /\.ht {
        #    deny  all;
     #}      
 }
}

配置完成后测试是否正常

/usr/local/nginx/sbin/nginx -t  

接着启动nginx

/usr/local/nginx/sbin/nginx  

若修改后配置文件或者将配置文件覆盖修改的 ,需要执行:

/usr/local/nginx/sbin/nginx -s reload  

修改后需要重启nginx,发现重启不了端口被占用,则用一下命令解决

netstat  grep 80   --查看端口80占用

sudo fuser -k 80/tcp   --关闭端口程序,然后重启即可

 

以上是关于Linux 下Nginx 的安装及负载均衡的简单配置的主要内容,如果未能解决你的问题,请参考以下文章

Nginx在linux环境下(centos7)的安装负载均衡设置

运维Linux下Nginx安装及使用

使用LVS实现负载均衡原理及安装配置详解

centos6 Nginx的安装部署与负载均衡配置

Linux下Nginx实现负载均衡

Windows下安装Nginx及负载均衡