Linux上nginx安装配置(配置虚拟主机,静态资源)

1.下载

进入http://nginx.org/en/download.html 下载nginx1.8.0版本(当前最新稳定版本)。

2.安装

安装前准备

nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境。

2.1 gcc

       安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:
yum install gcc-c++

2.2 PCRE

       PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

2.3 zlib

       zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel

2.4 openssl

       OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
       nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel

2.5开始安装

(1)导入nginx安装包
(a)Rz命令导入nginx安装包到usr/local/hadoop目录下面。
(2)解压:tar –zxvf nginx-1.8.0.tar.gz
(3)设置安装参数
设置前准备
(a)--prefix:设置nginx安装路径
创建名令:mkdir -p /usr/local/nginx
(b)/var/temp/nginx目录需要手动创建
创建名令:mkdir -p /var/temp/nginx
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
(4)编译
编译命令:make
(5)安装
安装命令:make install
(6)启动
(a)进入nginx安装目录sbin
(b)./nginx
(7)访问nginx

3.Nginx命令

3.1 Nginx启动流程

Nginx启动时候默认加载安装目录conf/nginx.conf
也可以指定加载配置路径:./nginx –c /usr/local/hadoop/nginx-1.8.0/conf/nginx.conf

3.2 启动命令

1)./nginx
2)./nginx –s reload 重启

3.3 停止命令

1)./nginx –s stop
解析:强制终止nginx进程。
2)./nginx –s quit
解析:凌迟处死,先让nginx把任务完成,然后终止。

4.nginx.conf配置

4.1基本参数

#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;   //events
}
http {   //http
    include       mime.types;
    default_type  application/octet-stream; 
    sendfile        on;
    keepalive_timeout  65;
    server {  //server
        listen       80;
        server_name  localhost;
       location / { //资源路径块
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
全局块:配置影响nginx全局参数,配置工作进程数。默认1
Events:工作连接数,默认1024.
http块:配置多个Server,配置虚拟主机,负载均衡等等。
Server块:配置服务器,服务器端口,服务器IP地址。
Location块:定位资源文件位置。

4.2基于域名的虚拟主机配置

4.2.1在local host中直接配置服务路径



4.2.2另一种配置方式upstream

http {
    include       mime.types;
    default_type  application/octet-stream; 
    sendfile        on;
    keepalive_timeout  65;
   upstream mytomcat1{
       server 192.168.66.66:8080;
    }
    upstream mytomcat2{
       server 192.168.66.66:8081;
    }
    server {
        listen       80;
       server_name  www.jd.com;
        location / {
            #root   html;
            #index  index.html index.htm;
          proxy_pass http://mytomcat1;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
1)客户机发送请求www.jd.com
2)Nginx接受请求:通过nginx.conf配置主机Server地址匹配:www.jd.com
3)匹配成功:就会去location下面寻找需要资源。
4)在location配置代理:去寻找代理服务器。http://mytomcat1
5)寻找名称是mytomcat1的upstream,Upstream负责把请求转发tomcat1。
通过端口访问的tomcat只会访问到tomcat的页面,需要在tomcat中加入初始化页面。
webapps/ROOT/index.jsp

4.3配置nginx代理静态资源






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章