nginx 安装和配置

1. 安装相关依赖

yum install readline-devel pcre-devel openssl-devel zlib-devel gcc gcc-c++  gd-devel libxml2-devel

2. 添加nginx第三方模块

1) pcre(rewrite正则表达式的支持):ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/           
2) libressl(加密解密):http://ftp.openbsd.org/pub/OpenBSD/LibreSSL/                    
3) openssl(加密解密):ftp://ftp.openssl.org/source
4) zlib(数据压缩):http://zlib.net/  http://zlib.net/zlib-1.2.8.tar.gz
5) ngx_cache_purge(缓存清除扩展模块):https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
6) nginx_upstream_check_module(负载均衡):https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
7) echo-nginx-module(模块,方便输出用的):https://github.com/openresty/echo-nginx-module/archive/v0.60.tar.gz
8)nginx:http://nginx.org/download/nginx-1.10.1.tar.gz

3. nginx编译安装

nginx 1.10.2 install on CentOS 6.x:
    ./configure --prefix=/usr/local/nginx \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-threads \
    --with-stream \
    --with-stream_ssl_module \
    --with-http_slice_module \
    --with-mail \
    --with-mail_ssl_module \
    --with-file-aio \
    --with-http_v2_module \
    --with-ipv6 \
    --with-pcre=../pcre-8.40 \
    --with-pcre-jit \
    --with-openssl=../libressl-2.5.1 \
    --with-zlib=../zlib-1.2.11/ \
    --add-module=../ngx_cache_purge-2.3/ \
    --add-module=../nginx_upstream_check_module-0.3.0/ \
    --add-module=../echo-nginx-module-0.60/ \
    --with-ld-opt=-lrt

    make
    make install

4.yum 安装nginx

yum install nginx

5.修改防火墙

/etc/sysconfig/iptables
/etc/init.d/iptables stop
/etc/init.d/iptables start
#80端口可以访问
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

6.nginx 配置文件结构

默认的配置文件:

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

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            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 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;
    #    }
    #}

}

7.nginx 文件结构

events {         #events块
   ...
}

http      #http块
{
    ...   #http全局块
    server        #server块
    { 
        ...       #server全局块
        location   #location块
        {
            ...
        }
        location 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局块
}
user www www;  #用户组
worker_processes 2;  #nginx要开启的进程数
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000;  #使用该选项可以绑定worker进程和CPU
pid /var/run/nginx.pid;  #nginx进程运行文件存放地址
events {
    accept_mutex on;  #设置网路连接序列化,防止惊群现象发生,默认为on
    multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off
    use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #最大连接数,默认为512
}
http {
    include       mime.types;   #文件扩展名与文件类型映射表
    default_type  application/octet-stream; #默认文件类型,默认为text/plain
    
    #access_log off; #取消服务日志    
    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
    access_log log/access.log myFormat;  #combined为日志格式的默认值
    
    sendfile on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
    sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
    
    keepalive_timeout 65;  #连接超时时间,默认为75s,可以在http,server,location块。
    error_page 404 https://www.baidu.com; #错误页
    
    #负载均衡
    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup; 
    }
    
    server {
        listen       80;   #监听端口
        root path;  #根目录
        index index.html;  #设置默认页
        server_name  a.com;   #监听地址     
        
        location  / {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
           proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表
           deny 127.0.0.1;  #拒绝的ip
           allow 172.18.5.54; #允许的ip           
        } 
    }
} 

转载于:https://www.cnblogs.com/sunyangphper/p/7502838.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值