Nginx安装部署

Nginx ("engine x") 是一个高性能的 HTTP  反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004104日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。201161日,nginx 1.0.4发布。

通过yum安装:

1.安装依赖包:

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

2.默认情况下,yum源中没有nginx,需要在/etc/yum.repos.d/目录下添加nginx.repos,并将下面的其中一个内容复制进去:

CentOS:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

RHEL:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/$releasever/$basearch/
gpgcheck=0
enabled=1
由于 CentOS 和 RHEL 之间的差别,系统的 Linux 定义了 $releasever 变量,需要根据你的操作系统的版本,将手动地将 $releasever 替换为 "5" (代表 5.x) 或 "6" (代表 6.x)

3.安装nginx

yum install nginx


 源码安装:

一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。

1.选定源码目录

选定目录 /usr/local/

 

cd /usr/local/

 

2.安装PCRE

cd /usr/local/

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz

tar -zxvf pcre-8.37.tar.gz

cd pcre-8.37

./configure

make

make install

 

3.安装zlib

cd /usr/local/ 

wget http://zlib.net/zlib-1.2.8.tar.gz

tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8

./configure

make

make install

 

4.安装ssl

 

cd /usr/local/

wget http://www.openssl.org/source/openssl-1.0.2c.tar.gz

tar -zxvf openssl-1.0.2c.tar.gz

./config

make

make install

 

5.安装nginx

 

Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:

 

cd /usr/local/

wget http://nginx.org/download/nginx-1.2.8.tar.gz

tar -zxvf nginx-1.2.8.tar.gz

cd nginx-1.2.8  

./configure --prefix=/usr/local/nginx \

--with-pcre=/usr/local/src/pcre-8.37 \    #指向解压的源码目录

--with-zlib=/usr/local/src/zlib-1.2.8 \      #指向解压的源码目录

--with-openssl=/usr/local/src/openssl-1.0.2c \    #指向解压的源码目录

--with-http_ssl_module \

--with-http_stub_status_module \   #启用 nginx 的 NginxStatus 功能,用来监控 Nginx 的当前状态

--user=nginx \

--group=nginx

make

make install

 备注:如果openssl,pcre,zlib是通过yum安装的,这里可以改用以下命令:

./configure 
  --user=nginx \
  --group=nginx \
  --with-http_ssl_module \
  --with-http_flv_module \
  --with-http_gzip_static_module 
make

make install

 

6.启动

确保系统的 80 端口没被其他程序占用,

/usr/local/nginx/sbin/nginx

 

检查是否启动成功:

netstat -ano|grep 80 有结果输入说明启动成功

 

 

打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。

 

7.重启

/usr/local/nginx/sbin/nginx –s reload

 

8.修改配置文件

cd /usr/local/nginx/conf

vi nginx.conf

 

9.常用配置

#nginx运行用户和组

user    www www;  

#启动进程,通常设置成和cpu的数量相等

worker_processes  4;

 

#全局错误日志及PID文件

pid /var/run/nginx.pid;

error_log  /var/log/nginx/error.log;

 

events {

        #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能

use epoll;

                   #单个后台worker process进程的最大并发链接数

        worker_connections  10240;

}

#设定http服务器,利用它的反向代理功能提供负载均衡支持

http {

        include       mime.types;

 

        default_type  application/octet-stream;

 

         error_page 400 403 500 502 503 504  /50x.html;

 

        index index.html index.shtml

 

        autoindex off;

 

         fastcgi_intercept_errors on;

 

        sendfile        on;

 

        # These are good default values.

        tcp_nopush      on;

        tcp_nodelay     off;

 

        # output compression saves bandwidth

        gzip  off;

         #gzip_static on;

        #gzip_min_length  1k;

        gzip_http_version 1.0;

        gzip_comp_level 2;

        gzip_buffers  4 16k;

        gzip_proxied any;

        gzip_disable "MSIE [1-6]\.";

        gzip_types  text/plain text/html text/css application/x-javascript application/xml application/xml+rss text/javascript;

        #gzip_vary on;

 

        server_name_in_redirect off;

 

#设定负载均衡的服务器列表

        upstream portals {

                  server 172.16.68.134:8082 max_fails=2 fail_timeout=30s;

                  server 172.16.68.135:8082 max_fails=2 fail_timeout=30s;

                            server 172.16.68.136:8082 max_fails=2 fail_timeout=30s;

                  server 172.16.68.137:8082 max_fails=2 fail_timeout=30s;

        }

 

        #upstream overflow {

         #       server 10.248.6.34:8090 max_fails=2 fail_timeout=30s;       

         #       server 10.248.6.45:8080 max_fails=2 fail_timeout=30s;       

        #}

 

        server {

                                     #侦听8080端口

                listen       8080;

                server_name  127.0.0.1;

 

                   #403404页面重定向地址

                   error_page  403 = http://www.e100.cn/ebiz/other/217/403.html;

                   error_page  404 = http://www.e100.cn/ebiz/other/218/404.html;

                   proxy_connect_timeout      90;

                   proxy_send_timeout         180;

                   proxy_read_timeout         180;

 

                   proxy_buffer_size 64k;

                   proxy_buffers 4 128k;

                   proxy_busy_buffers_size 128k;

 

 

                   client_header_buffer_size 16k;

                   large_client_header_buffers 4 64k;

 

                #proxy_send_timeout         3m;

                #proxy_read_timeout         3m;

                #proxy_buffer_size          4k;

                #proxy_buffers              4 32k;

 

                proxy_set_header Host $http_host;

                proxy_max_temp_file_size 0;

                #proxy_hide_header Set-Cookie;

                  

         #       if ($host != 'www.e100.cn' ) {

         #                rewrite ^/(.*)$ http://www.e100.cn/$1 permanent;

         #       }

 

 

               location / {

                       deny all;

               }

 

                   location ~ ^/resource/res/img/blue/space.gif {

                    proxy_pass http://tecopera;

               }

 

               location = / {

                   rewrite ^(.*)$  /ebiz/event/517.html last;

               }

 

 

 

                   location = /ebiz/event/517.html {

                    add_header Vary Accept-Encoding;

                    root /data/web/html;

                    expires 10m;

               }

 

 

 

 

               location = /check.html {

                    root /usr/local/nginx/html/;

                    access_log off;

               }

 

               location = /50x.html {

                    root /usr/local/nginx/html/;

                    expires 1m;

                    access_log off;

               }

 

 

              location = /index.html {

                       add_header Vary Accept-Encoding;

#定义服务器的默认网站根目录位置

                    root /data/web/html/ebiz;

                    expires 10m;

               }

#定义反向代理访问名称

                   location ~ ^/ecps-portal/* {

                   # expires 10m;

#重定向集群名称

                    proxy_pass http://portals;

                    #proxy_pass http://172.16.68.134:8082;

               }

 

                   location ~ ^/fetionLogin/* {

                   # expires 10m;

                    proxy_pass http://portals;

                    #proxy_pass http://172.16.68.134:8082;

                }

 

                   #location  ~ ^/business/* {                                                                      

                #   # expires 10m;                                                                                

                #    proxy_pass http://172.16.68.132:8088;                                                                   

                #    #proxy_pass http://172.16.68.134:8082;                                                       

                #}

 

                   location ~ ^/rsmanager/* {

                    expires 10m;

                    root /data/web/;

                    #proxy_pass http://rsm;

               }

#定义nginx处理的页面后缀

                   location ~* (.*)\.(jpg|gif|htm|html|png|js|css)$  {

                            root /data/web/html/;

#页面缓存时间为10分钟

                         expires 10m;

                   }

 

#设定查看Nginx状态的地址     

               location ~* ^/NginxStatus/ {

                    stub_status on;

                    access_log off;

                    allow 10.1.252.126;

                    allow 10.248.6.49;

                    allow 127.0.0.1;

                    deny all;

               }

         #       error_page   405 =200 @405;

         #       location @405

         #       {

         #                proxy_pass http://10.248.6.45:8080;

         #       }  

 

               access_log  /data/logs/nginx/access.log combined;

               error_log   /data/logs/nginx/error.log;

        }

         server {

                listen       8082;

 

                server_name  _;

               location = /check.html {

                    root /usr/local/nginx/html/;

                    access_log off;

               }

                  

        }

         server {

                   listen       8088;

                   server_name  _;

                   location ~ ^/* {

                   root /data/web/b2bhtml/;

                   access_log off;

         }                

         }

        server {

                listen       9082;

                server_name  _;

 

        #        location ~ ^/resource/* {

        #            expires 10m;

         #           root /data/web/html/;

         #       }

 

                location  / {

                     root /data/web/html/sysMaintain/;

                       if (!-f $request_filename) {

                            rewrite ^/(.*)$ /sysMaintain.html last;

                           }

                }

        }

 

}

注:编译好的nginx可以通过 /usr/local/nginx/nginx -V  (具体路径以安装的为主) 查看编译时候的参数

 

启动nginx

 /usr/local/nginx/nginx #不指定配置文件地址

 /usr/local/nginx/nginx -c /usr/local/nginx/nginx.conf #指定配置文件地址

停止服务

sudo kill `cat /usr/local/nginx/nginx.pid`

检测配置文件  

/usr/local/nginx/nginx -t

 

重新加载配置文件(不停止服务)

/usr/local/nginx/nginx -s reload

 

打开目录浏览功能

复制代码
location / {

    autoindex on;#打开目录列表
    autoindex_exact_size off; #on显示文件的确切大小,off则会用M、G等单位
    autoindex_localtime on; #显示文件服务器时间,off显示GMT时间
    root   html;
    index  index.html index.htm;
}
复制代码

 

Nginx编译参数解析

复制代码
–prefix #nginx安装目录,默认在/usr/local/nginx
–pid-path #pid问件位置,默认在logs目录
–lock-path #lock问件位置,默认在logs目录
–with-http_ssl_module #开启HTTP SSL模块,以支持HTTPS请求。
–with-http_dav_module #开启WebDAV扩展动作模块,可为文件和目录指定权限
–with-http_flv_module #支持对FLV文件的拖动播放
–with-http_realip_module #支持显示真实来源IP地址
–with-http_gzip_static_module #预压缩文件传前检查,防止文件被重复压缩
–with-http_stub_status_module #取得一些nginx的运行状态
–with-mail #允许POP3/IMAP4/SMTP代理模块
–with-mail_ssl_module #允许POP3/IMAP/SMTP可以使用SSL/TLS
–with-pcre=../pcre-8.11 #注意是未安装的pcre路径
–with-zlib=../zlib-1.2.5 #注意是未安装的zlib路径
–with-debug #允许调试日志
–http-client-body-temp-path #客户端请求临时文件路径
–http-proxy-temp-path #设置http proxy临时文件路径
–http-fastcgi-temp-path #设置http fastcgi临时文件路径
–http-uwsgi-temp-path=/var/tmp/nginx/uwsgi #设置uwsgi 临时文件路径
–http-scgi-temp-path=/var/tmp/nginx/scgi #设置scgi 临时文件路径
复制代码

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐