## Nginx 配置

来源:互联网 发布:深圳市网络诈骗 编辑:程序博客网 时间:2024/06/06 01:43

Nginx 配置

Nginx (“engine x”) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行的。

1.准备工作

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

2.下载并安装

# mkdir nginx-src && cd nginx-src  # wget http://nginx.org/download/nginx-1.7.3.tar.gz  # tar xzf nginx-1.7.3.tar.gz   # cd nginx-1.7.3  # ./configure  # make  # make install  # whereis nginx  nginx: /usr/local/nginx  

跳转至安装路径,查看安装版本

# ./nginx -h  

启动

nginx

停止

nginx -s stop

3.Nginx常用功能 摘抄自张书豪

1.Http代理,反向代理

Nginx在做反向代理时,提供性能稳定,并且能够提供配置灵活的转发功能。Nginx可以根据不同的正则匹配,采取不同的转发策略,比如图片文件结尾的走文件服务器,动态页面走web服务器,只要你正则写的没问题,又有相对应的服务器解决方案,你就可以随心所欲的玩。并且Nginx对返回结果进行错误页跳转,异常判断等。如果被分发的服务器存在异常,他可以将请求重新转发给另外一台服务器,然后自动去除异常服务器。
2.负载均衡
Nginx提供的负载均衡策略有2种:内置策略和扩展策略。内置策略为轮询,加权轮询,Ip hash。扩展策略,就天马行空

Ip hash算法,对客户端请求的ip进行hash操作,然后根据hash结果将同一个客户端ip的请求分发给同一台服务器进行处理,可以解决session不共享的问题。

3.web缓存
ginx可以对不同的文件做不同的缓存处理,配置灵活,并且支持FastCGI_Cache,主要用于对FastCGI的动态程序进行缓存。配合着第三方的ngx_cache_purge,对制定的URL缓存内容可以的进行增删管理。

4.Nginx 配置文件结构

apache的配置文件,它的相对比较清晰和简单,之前觉得很难,现在沉下心来想想,其实很简单。大致的分块下,基本就分为以下几块:

mainevents   {  ....}http        {  ....  upstream myproject {    .....  }  server  {    ....    location {        ....    }  }  server  {    ....    location {        ....    }  }  ....}

nginx配置文件主要分为六个区域:
main(全局设置)、events(nginx工作模式)、http(http设置)、
sever(主机设置)、location(URL匹配)、upstream(负载均衡服务器设置)。
下面粘贴一张真实用例。

#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  alan.com;        #charset koi8-r;     #   access_log  logs/host.access.log  main;        location / {            root   html;            index  index.html index.htm;        location = /hello.html {         index  hello.html;    }        location /rtis-ois {        proxy_set_header Accept-Encoding 'gzip';        proxy_set_header Access-Control-Allow-Origin '*';                proxy_pass http://localhost:8080/RTIS-OIS-0.0.1;    }        location /diocm-viewer {        proxy_set_header Accept-Encoding 'gzip';    proxy_set_header Access-Control-Allow-Origin '*';       proxy_pass http://139.217.25.228:8080/dicom-viewer;    }        #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       8081;    #   listen       127.0.0.1:8080;        #server_name  a.alan.com;    #access_log /usr/local/nginx/a.alan.com.log main;    #root /usr/data/a.alan.com     #   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;    #    }    }}
0 0
原创粉丝点击