• nginx源码安装centos7


    安装平台:centos7.3

    1、下载包到指定目录

    wget http://nginx.org/download/nginx-1.16.1.tar.gz -P /tmp

    2、解压包

    tar -zxvf /tmp/nginx-1.16.1.tar.gz -C /tmp

    3、安装依赖包

    yum -y install gcc
    yum -y install zlib-devel
    yum -y install pcre-devel
    yum -y install openssl-devel

    4、进行nginx配置编译安装

    cd /tmp/nginx-1.16.1
    ./configure
    make
    make install

    5、进入默认路径启动

    cd /usr/local/nginx/sbin
    ./nginx

    6、网页访问ip (不能访问先关闭selinux、iptables)

    setenforce 0  #临时管理selinux
    systemctl stop firewalld 

    7、改端口

    cd /usr/local/nginx/conf
     vi nginx.conf

    8、配置开机启动,两种方法取其一即可

    方法一:
    echo '/usr/local/nginx/sbin/nginx'>>/etc/rc.local
    chmod u+x /etc/rc.local
    方法二:
    [root@localhost ]# cat << EOF > /lib/systemd/system/nginx.service #创建Nginx服务系统启动文件
    [Unit]
    Description=nginx
    After=network.target

    [Service]
    Type=forking
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s quit
    PrivateTmp=true

    [Install]
    WantedBy=multi-user.target
    EOF

    systemctl daemon-reload && systemctl start nginx && \
    systemctl enable nginx && systemctl status nginx

    9、nginx配置简单的负载均衡

    cat <<EOF >/usr/local/nginx/conf/nginx.conf
    worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { proxy_pass http://192.168.1.10:8080; proxy_pass http://192.168.1.10:8081; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
    EOF

    下面介绍一些常用配置

     10、正常代理

    // 下面这些代码的意思是将本机80号端口接受来的消息转发给 192.168.10.10 的 80 号端口
    upstream www.wukc.com{                          //这里的域名随便填,但是要和下面的对应
            server 192.168.10.10:80;            //真正提供服务的服务器的ip地址和端口
        }   
    server{
    listen  80;                                  // 监听80号端口发过来的消息
    location  /{
             proxy_pass http://www.wukc.com;
             index  index.html index.php;
             }   
    }

     11、根据不同端口进行转发 

    //这些代码的意思是将80端口接收到的信息转发给 192.168.10.10的80端口,而将接受到的 8080端口的信息转发给 192.168.10.20的8080端口
    upstream www.wukc.com{
            server 192.168.10.10:80;
     }
     
    upstream www.wukc2.com{
            server 192.168.10.20:8080;
     }
     
    server{
    listen 80;
    location /{
        proxy_pass http://www.wukc.com;
        index  index.html index.php;
             }
    }
     
    server{
    listen 8080;
    location /{
        proxy_pass http://www.wukc2.com;
        index  index.html index.jsp;
            }
    }

    12、四种负载均衡

    1:轮询:每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除
    upstream www.wukc.com{
            server 192.168.10.10:80;
            server 192.168.10.20:80;
        }
    server{
    listen 80;
    location /{
      proxy_pass http://www.wukc.com;
      index  index.html index.php index.jsp;
      }
    }
     
    2:ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
    upstream www.wukc.com{
            ip_hash;
            server 192.168.10.10:80;
            server 192.168.10.20:80;
        }
    server{
    listen 80;
    location /{
      proxy_pass http://www.wukc.com;
      index  index.html index.php index.jsp;
      }
    }
     
    3:weight:指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
    upstream www.wukc.com{
            server 192.168.10.10:80 weight=10;
            server 192.168.10.20:80 weight=20;
        }
    server{
    listen 80;
    location /{
      proxy_pass http://www.wukc.com;
      index  index.html index.php index.jsp;
      }
    }
     
    4: fair : 按后端服务器的响应时间来分配请求,响应短的服务器优先分配
    upstream www.wukc.com{
            server 192.168.10.10:80 weight=10;
            server 192.168.10.20:80 weight=20;
            fair;
        }
    server{
    listen 80;
    location /{
      proxy_pass http://www.wukc.com;
      index  index.html index.php index.jsp;
      }
    }

    13、本地转发代理(基于不同后缀)

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /usr/share/nginx/html;
            index  index.php index.html index.htm;
        }
       
        location ~ \.php$ {              //当请求的后缀是以 .php结尾的话,将流量转发给本地的800端口
            proxy_pass   http://127.0.0.1:800;
        }
        location ~ \.jsp$ {            //当请求的后缀是以 .jsp结尾的话,将流量转发给本地的8080端口
            proxy_pass   http://127.0.0.1:8080;
        }
        location ~ \.(jpg|png)$ {     //当请求的后缀是以 .jpg或.png 结尾的话,则请求 /img 目录下
           root  /img;
        }
    }

     14、不同访问路径转发到不同服务配置

    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
    #配置两个upstream,转发到不同服务
    upstream kubesphere{
       server 172.21.210.10:30880;
    }
    upstream harbor{
       server 172.21.210.20:80;
    }
    
        server {
            listen       443;
            server_name  localhost;
            location /kubesphere {              #访问路径可以和upstream不一样
                proxy_pass http://kubesphere;   #该名称为上方upstream名称,转发到上面的upstream
            }
           
            location /harbor {
                proxy_pass http://harbor;
            }
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    
    }

     15、nginx指定访问网页

            location = /hello.html {
               root /usr/local/nginx/html;      --默认目录可以不写这行-不是默认目录需要写nginx的根目录
                index hello.html;               --需要编写hello.html文件到根目录下
            }
    
    curl http://localhost/hello.html      --直接返回hello.html网页内容 #其实就是500网页指定的配置

     16、server解析

    server{
    listen 80;
    server name 192.168.8.18 cszhi.com;
    index index.html index.htm index.php;
    root /wwwroot/www.cszhi.com
    charset gb2312;
    access_log logs/www.ixdba.net.access.log main;
    
    server标志定义虚拟主机开始,
    listen用于指定虚拟主机的服务端口,
    server_name用来指定IP地址或者域名,多个域名之间用空格分 开。
    index用于设定访问的默认首页地址,
    root指令用于指定虚拟主机的网页根目录,这个目录可以是相对路径,也可以是绝对路径。
    Charset用于 设置网页的默认编码格式。
    access_log用来指定此虚拟主机的访问日志存放路径,
    main用于指定访问日志的输出格式。

    17、location URL匹配配置

    URL地址匹配是进行Nginx配置中最灵活的部分。 
    location支持正则表达式匹配,也支持条件判断匹配,用户可以通过location指令实现Nginx对动、静态网页进行过滤处理。
    使用location URL匹配配置还可以实现反向代理,用于实现PHP动态解析或者负载负载均衡。
    以下这段设置是通过location指令来对网页URL进行分析处理,
    所有扩展名以.gif、.jpg、.jpeg、.png、.bmp、.swf结尾的静态文件都交给nginx处理,
    而expires用来指定静态文件的过期时间,这里是30天。
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
    root /wwwroot/www.cszhi.com;
    expires 30d;
    }
    
    以下这段设置是将upload和html下的所有文件都交给nginx来处理,当然,
    upload和html目录包含在/web/wwwroot/www.cszhi.com目录中。
    
    location ~ ^/(upload|html)/ {
    root /web/wwwroot/www.cszhi.com;
    expires 30d;
    }
    
    最后这段设置中,location是对此虚拟主机下动态网页的过滤处理,也就是将所有以.jsp为后缀的文件都交给本机的8080端口处理。
    
    location ~ .*.php$ {
    index index.php;
    proxy_pass http://localhost:8080;
    }

     18、nginx配置访问txt、npg、jpg等图片

            location ~ .*\.(txt|png)$ {        #静态文件格式
               root /usr/local/nginx/html/;    #图片或者txt存储路径
            }
    直接访问 http://localhost/tu_name.png   访问到图片
    做一个决定,并不难,难的是付诸行动,并且坚持到底。
  • 相关阅读:
    一些简单的问题
    WebRTC的 windows 7 环境搭建
    HTML常用标签
    参考C#编程规范
    C#窗体调用(转载)
    java中的小知识(不断更行中。。。。。)
    CF1483E Vabank 题解
    CF755G PolandBall and Many Other Balls 题解
    CF1483D Useful Edges 题解
    CF1368F Lamps on a Circle 题解
  • 原文地址:https://www.cnblogs.com/wukc/p/13236699.html
Copyright © 2020-2023  润新知