Nginx的安装配置及负载均衡的简单实现(nginx.conf的配置)

在linux中安装配置nginx,安装nginx之前需要先安装gcc、zlib-devel、openssl-devel和pcre-devel软件库
一、安装nginx的先行软件的安装
1、gcc(GNU Compiler Collection)是用于编程开发的自由编译器,它可以编译的语言有C、C++、Java、Fortran等,
   此外Autoconf和Automake也是需要安装的,下面yum命令中的gcc-c++是为gcc提供c++语言特性支持的,
   其yum安装命令为:yum -y install gcc gcc-c++ autoconf automake
2、nginx的gzip模块需要zlib库,rewrite模块需要pcre库,ssl功能需要openssl库,
   其yum安装命令为:yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
二、nginx的下载安装
1、nginx的下载,其地址:http://www.nginx.net ,然后在这句话nginx-1.14.0 stable version has been released中点击
   nginx-1.14.0,如果下载linux版本,点击nginx-1.14.0进入下载;如果下载windows版本,点击nginx/Windows-1.14.0进入下载。


2、nginx的windows安装,将下载好的nginx-1.14.0.zip文件解压到一个不包含空格的路径中,例如D:\nginx,然后点击
   "开始"->"所有文件"->"附件"->"运行"->"输入cmd指令"->"回车",在dos命令窗口中执行以下命令即可启动nginx,
   cd D:\nginx(点击回车)
   start nginx(点击回车)
   如果要对启动的nginx进程进行控制,可以使用如下DOS命令:
   nginx -s [ stop | quit | reopen | reload ]


3、ngnix的linux(CentOS 7)安装,将下载好的nginx-1.12.2.tar.gz放在自己喜欢的位置,然后执行下面的命令:
   tar zxvf nginx-1.14.0.tar.gz(点击回车)
   cd nginx-1.14.0(点击回车)
   ./configure(点击回车)
   make(点击回车)
   sudo make install(点击回车)
   以上命令执行完毕后,nginx将被默认安装到/usr/local/nginx目录下。可以通过./configure --help命令查看nginx可选择的编译选项。
   
三、Linux中nginx的启动、停止、平滑重启
1、nginx的启动
   启动nginx,可以执行以下命令。假设nginx安装在/usr/local/nginx目录下,那么启动nginx的命令就是:
   /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (点击回车)
   参数"-c"制定了配置文件的路径,如果不加"-c"参数,nginx会默认加载其安装目录的conf子目录中的nginx.conf文件,
   在本例中即:/usr/local/nginx/sbin/nginx/conf/nginx.conf
   
2、nginx的停止
   nginx的停止方法有很多种,一般通过发送系统信号nginx主进程的方式来停止nginx。
   我们可以通过ps命令来查找nginx的主进程号:ps -ef | grep nginx
   这时候屏幕上会显示进程的信息,1个nginx进程的备注信息为"master process",表示它为主进程,其它备注为"worker process"的进程为子进程。
   以我的为例:主进程号为11818。
   如果pid文件(nginx.pid)存放的路径没有在nginx.conf配置文件中进行配置,那么其默认存放在nginx安装目录的logs目录下(/usr/local/nginx/logs/nginx.pid)
   (1)从容停止nginx
      kill -QUIT 11818 或 kill -QUIT '/usr/local/nginx/logs/nginx.pid'
   (2)快速停止nginx
      kill -TERM 11818 或 kill -TERM '/usr/local/nginx/logs/nginx.pid'
  kill -INT 11818 或 kill -INT '/usr/local/nginx/logs/nginx.pid'
   (3)强制停止所有nginx进程
      pkill -9 nginx
  
3、nginx的平滑重启
   如果改变了nginx的配置文件(nginx.conf),想重启nginx,同样可以通过发送系统信号给nginx主进程的方式来进行。
   不过,重启之前,要确认nginx配置文件(nginx.conf)的语法是正确的,否则nginx将不会加载新的配置文件。通过以下命令可以判断nginx配置文件是否正确:
   /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf(点击回车)
   如果配置文件正确,屏幕将提示以下两行信息:
   nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
   nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
   这时候,就可以平滑重启nginx了。
   kill -HUP 11818 或 kill -HUP '/usr/local/nginx/logs/nginx.pid'
   当nginx接收到HUP信号时,它会尝试先解析配置文件(如果指定配置文件,就使用指定的,否则使用默认的),如果成功,就应用新的配置文件(例如:重新打开配置
   文件或监听的套接字)。之后,nginx运行新的工作进程并从容关闭旧的工作进程。通知工作进程关闭监听套接字,但是继续为当前连接的客户提供服务。
   所有客户端的服务完成后,旧的工作进程被关闭。如果新的配置文件应用失败,nginx将继续使用旧的配置进行工作。
4、nginx的信号控制
   (1)TERM和INT 快速关闭
   (2)QUIT 从容关闭
   (3)HUP 从容重启,重新加载配置文件
   (4)USR1 重新打开日志文件,在切割日志时用途较大
   (5)USR2 平滑升级可执行程序

   (6)WINCH 从容关闭工作进程

5、nginx的平滑升级

四、nginx实现简单的负载均衡的配置,nginx.conf的配置如下

#user  nobody;
worker_processes  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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    upstream admin.mazhen.com {
         server 120.52.146.171:8080 weight=3;
         server 120.52.146.171:8081 weight=2;
    }
    server {
        listen       80;
        

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #其中admin.mazhen.com对应着upstream设置的集群名称
             proxy_pass http://admin.mazhen.com;
             #设置主机头和客户端真实地址,以便服务器获取客户端真实IP
             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_connect_timeout    600;
             proxy_read_timeout       600;
             proxy_send_timeout       600;
        }

        #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 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;
    #    }
    #}


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

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

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

参考:https://blog.csdn.net/u010028869/article/details/50484976

猜你喜欢

转载自blog.csdn.net/mameng1988/article/details/80182825
今日推荐