Linux下安装nginx

一.安装依赖包

每次安装软件都必须,默认会少几个包,必须安装

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

 

二.下载nginx和相关模块

nginx:可以去官网 http://nginx.org/ 下载最新版

curl -O http://nginx.org/download/nginx-1.15.8.tar.gz

 缓存模块ngx_cache_purge:可以去 http://labs.frickle.com/nginx_ngx_cache_purge/ 下载最新版本

curl -O http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
#解压
tar zxvf nginx-1.15.8.tar.gz
tar zxvf ngx_cache_purge-2.2.tar.gz

 

三.编译安装 

./configure --user=www --group=www \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_ssl_module \
--add-module=../ngx_cache_purge-2.3 make & make install
#扁平版
./configure --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --add-module=../ngx_cache_purge-2.3

 

安装好之后,默认的nginx根目录为 /usr/local/nginx  

 

四.配置示例

nginx配置的例子很多,就不赘述,直接问度娘吧.

使用缓存的例子:

proxy_cache_path   /tmp/cache   keys_zone=tmpcache:10m;
location / {
  proxy_pass        http://127.0.0.1:8000;
  proxy_cache        tmpcache;    proxy_cache_key        $uri$is_args$args;
  proxy_cache_purge    PURGE from 127.0.0.1;
}

 

服务器配置

user  www www;
worker_processes  auto;
events {
    use epoll;
    worker_connections  65535;
    multi_accept on;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;
    sendfile      on;
    tcp_nopush    on;
    tcp_nodelay   on;
    keepalive_timeout 600;
    client_header_timeout 300;
    client_body_timeout 300;
    reset_timedout_connection on;
    send_timeout 300;

add_header X-Frame-Options SAMEORIGIN; gzip on; gzip_min_length 2k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 3; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php; gzip_vary on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]\."; access_log off; include /usr/local/nginx/conf/vhost/*.conf; }

然后在 vhost下所有.conf的,就都是配置文件了

 

标准http配置

server {
        listen       80;
        server_name  127.0.0.1 10.10.255.101 default;
        charset utf-8;
        #root   /u02/www/html/iot;
        index   index.html index.htm;
        location ^~ /res/ {
                alias /u02/www/html/res/;
                index index.html;
                #return 601;
        }
        location / {
                root /u02/www/html/iot;
        }
        access_log  /u02/log/nginx/default.access.log;
        error_log  /u02/log/nginx/default.error.log;
}

alias / root 区别
location /img/ {
    alias /var/www/image/;
}
#若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件
location /img/ {
    root /var/www/image;
}
#若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件

  所以配置额外资源,就需要使用alias , 同时,通用配置下就不能有res资源 , 应为会先被匹配访问

 

增加页面header , 例子为index不进行缓存

location = /index.html {
 add_header Cache-Control "no-cache, no-store";
}

 

 

五.Stream转发配置示例

如果需要引入stream也就是tcp转发 , 则在nginx.conf最下面增加配置

stream {
    include /usr/local/nginx/conf/vhost/*.tcp;
}

那么vhost下所有的.tcp,就是tcp转发的内容

转发配置示例:

#代理MSSQL数据库
upstream mssql_basedata {
   server 192.168.2.46:1433 max_fails=3 fail_timeout=30s;
}
server {
  listen 15431;
  proxy_connect_timeout 1s;
  proxy_timeout 3s;
  proxy_pass mssql_basedata;
}

 

#代理Windows远程桌面
server {
  listen 12112 so_keepalive=on;
  proxy_connect_timeout 30s;
  proxy_timeout 1m;
  tcp_nodelay on;
  proxy_pass 10.100.1.11:3389;
}

 

五.负载均衡配置示例

#第一种 权重配置法
#weight 权重 默认为1.weight越大,负载的权重就越大
#max_fails 允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
#fail_timeout max_fails次失败后,暂停的时间
#down down 表示单前的server暂时不参与负载
#backup 其它所有的非backup机器down或者忙的时候,请求backup机器
upstream serverlist {
    server 192.168.196.130 weight=1 fail_timeout=20s max_fails=1;
    server 192.168.196.132 weight=2 fail_timeout=20s max_fails=1;
}

#第二种 iphash配置法
#此方法权重配置无效
upstream serverlist {
    ip_hash;
    server 192.168.196.130 fail_timeout=20s max_fails=1;
    server 192.168.196.132 fail_timeout=20s max_fails=1;
}

#使用负载
server {
    listen 80;
    server_name xxx.com;
    index index.html index.htm index.php;
    location / {
        proxy_pass http://serverlist;
    }
}

 

负载参考资料 :

1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 

upstream backserver {
server 192.168.0.14;
server 192.168.0.15;
}
2、指定权重
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 

upstream backserver {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}
3、IP绑定 ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 

upstream backserver {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
4、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。 

upstream backserver {
server server1;
server server2;
fair;
}
5、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 

upstream backserver {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
在需要使用负载均衡的server中增加 

proxy_pass http://backserver/;
upstream backserver{
ip_hash;
server 127.0.0.1:9090 down; (down 表示单前的server暂时不参与负载)
server 127.0.0.1:8080 weight=2; (weight 默认为1.weight越大,负载的权重就越大)
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器)
}
max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
fail_timeout:max_fails次失败后,暂停的时间

 

=====

Nginx 正向代理 : 

https://www.cnblogs.com/hei-ma/p/9722478.html

posted on 2015-03-30 15:11  太清  阅读(10043)  评论(0编辑  收藏  举报