Linux Nginx 全量安装配置

Posted 高国藩

tags:

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

  • 下载反向代理压缩包
    所有的文件下载链接地址,请到文章最下面查找;
## 下载安装包 wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
## 安装解压命令 yum install unzip
## 执行解压操错 unzip master
  • 安装依赖库文件
# 安装zlib第三方库
wget http://dl.download.csdn.net/down10/20131007/9be6c56fd26170af17babe1769b906f5.gz?response-content-disposition=attachment%3Bfilename%3D%22zlib-1.2.8.tar.gz%22&OSSAccessKeyId=9q6nvzoJGowBj4q1&Expires=1494242459&Signature=QYeMKOSBbyvWz%2FgLl5AsEXR9Yw0%3D
tar zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make && make install

# 安装pcre第三方库
wget https://jaist.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz
tar zxvf pcre-8.40.tar.gz
cd pcre-8.40
./configure
make && make install

# 安装c++编译环境
yum install -y gcc gcc-c++

# 安装ssl环境信息
yum -y install pcre-devel openssl openssl-devel
  • 下载并安装Nginx
$ wget http://nginx.org/download/nginx-1.7.4.tar.gz
$ tar -xzvf nginx-1.7.4.tar.gz
$ cd /nginx-1.7.4
$ patch -p1 < /home/nginx_upstream_check_module-master/check_1.7.2+.patch
$ ./configure --prefix=/home/nginx-devls --user=nobody --group=nobody --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --add-module=/home/nginx_upstream_check_module-master
$ make
$ make install
## 注:因nginx版本更新,1.2以上版本的nginx,补丁为check_1.2.1+.patch
出现下面命令时候,意味着安装即将成功 。。。。
configuring additional modules
adding module in /home/nginx_upstream_check_module-master
checking for ngx_http_upstream_check_module ... found
 + ngx_http_upstream_check_module was configured
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for OpenSSL library ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/home/nginx-devls"
  nginx binary file: "/home/nginx-devls/sbin/nginx"
  nginx configuration prefix: "/home/nginx-devls/conf"
  nginx configuration file: "/home/nginx-devls/conf/nginx.conf"
  nginx pid file: "/home/nginx-devls/logs/nginx.pid"
  nginx error log file: "/home/nginx-devls/logs/error.log"
  nginx http access log file: "/home/nginx-devls/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
  • 配置文件信息

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

    #limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s;

    upstream backend  
        #ip_hash;
        
        server 47.107.76.88:8080 weight=1 max_fails=5 fail_timeout=3600s ;
        check interval=3000 rise=2 fall=5 timeout=1000;
    

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  30;

    #gzip  on;

    server 
        listen       80;
        listen [::]:80 ipv6only=on;
        server_name  www.sumernetwork.com;


        location /node 
            check_status;
            access_log   on;
            #allow SOME.IP.ADD.RESS;
            #deny all;
         

        location /rice-main-business 
            #limit_req zone=one burst=5;
            proxy_redirect  off;
            proxy_pass http://backend ;
            proxy_next_upstream error timeout invalid_header http_404;

            proxy_connect_timeout 10s; #后端服务器连接的超时时间_发起握手等候响应超时时间
            proxy_read_timeout 10s;  #后端服务器处理请求的时间
            
            #nginx反向代理配置时,一般会添加下面的配置:

            proxy_set_header Host $host;  
            proxy_set_header X-Real-IP $remote_addr;  
            proxy_set_header REMOTE-HOST $remote_addr;  
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  

            #weight:轮询权值也是可以用在ip_hash的,默认值为1
            #max_fails:允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。
            #fail_timeout:有两层含义,一是在 30s 时间内最多容许 2 次失败;二是在经历了 2 次失败以后,30s时间内不分配请求到这台服务器。
            #backup:备份机器。当其他所有的非backup机器出现故障的时候,才会请求backup机器
            #max_conns: 限制同时连接到某台后端服务器的连接数,默认为0即无限制。因为queue指令是commercial,所以还是保持默认吧。
            #proxy_next_upstream:这个指令属于 http_proxy 模块的,指定后端返回什么样的异常响应时,使用另一个realserver
        

        location / 
            index index.html;
        

    




  • 平滑启动命令
检查配置信息是否有误 ./nginx -t
平滑启动服务器 ./nginx -s reload
  • ssl相关配置信息

#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;
    keepalive_timeout  65;

    upstream backend  
        ip_hash;
        server 127.0.0.1:8080 weight=1 max_fails=5 fail_timeout=3600s ;
        check interval=3000 rise=2 fall=5 timeout=1000;
    

    server 
        listen 80;
        server_name  www.job.com;
        return 307 https://$host$request_uri;    ## 注意这一行,是解决请求方法Post->变成Get的关键。
    

    server 
        listen 443 default ssl;
        ssl on;
        ssl_certificate /usr/cert/2420415_www.job.com.pem;
        ssl_certificate_key /usr/cert/2420415_www.job.com.key;
        server_name www.job.com;
        location / 
            #limit_req zone=one burst=5;
            proxy_redirect  off;
            proxy_pass http://backend ;
            proxy_next_upstream error timeout invalid_header http_404;

            proxy_connect_timeout 10s; 
            proxy_read_timeout 10s;  

            proxy_set_header Host $host;  
            proxy_set_header X-Real-IP $remote_addr;  
            proxy_set_header REMOTE-HOST $remote_addr;  
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
                
    



以上是关于Linux Nginx 全量安装配置的主要内容,如果未能解决你的问题,请参考以下文章

linux nginx配置

Linux源码安装nginx并配置

Linux——在Linux系统上安装启动和配置Nginx

Linux——在Linux系统上安装启动和配置Nginx

Linux环境下配置Nginx

Linux环境下配置Nginx