nginx 安装及使用

  1. 安装
    如果是windows系统直接解压可用,如果是linux则需要安装,及具备安装环境。
    linux系统安装:
    1). 若没有安装gcc,则需要安装gcc环境,运行命令:yum install gcc-c++;
    2).安装第三方开发包。
  • PCRE :nginx 用来解析正则表达式等。yum install -y pcre-devel
  • zlib http包压缩和解压方式。yum install -y zlib zlib-devel
  • openssl 安.全套接字层密码库。yum install -y openssl

openssl-devel
安装nignx:
1.把nginx压缩包上传到linux系统上。
2.解压缩:tar zxf nginx-1.8.0.tar.gz
3.进入nginx目录,用configure命令生成Makefile文件,
./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
注意:启动nginx之前,上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
4.输入make执行编译。
5.执行make install 安装。
6.创建目录 :mkdir /var/temp/nginx/client -p
7.启动 ./nginx
8.查看进程 ps aux | grep nignx
9.停止 ./nginx -s stop.

2.使用
1.配置文件:nginx.conf由多个块组成,最外面的块是main,main包含Events和HTTP,HTTP包含upstream和多个Server,Server又包含多个location:main(全局设置)、server(主机设置)、upstream(负载均衡服务器设置)和 location(URL匹配特定位置的设置)


#user  nobody; // 指定Nginx Worker进程运行用户以及用户组
worker_processes  1; // 指定了Nginx要开启的进程数,一般和cpu数量一至

//日志存放路径
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

//pid日志存放路径
#pid        logs/nginx.pid;


events {
    use epoll; //指定nginx的工作模式
    worker_connections  1024; //最大连接数,由worker_processes和worker_connections决定,即Max_client=worker_processes*worker_connections。
}


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;
    
	//upstream是Nginx的HTTP Upstream模块,这个模块通过一个简单的调度算法来实现客户端IP到后端服务器的负载均衡。值有默认轮询,weight;
	upstream cszhi.com{
ip_hash;
server 192.168.8.13:8009 max_fails=3 fail_timeout=20s;
server 192.168.8.146:8080;
}
    sendfile        on;  //用于是否开启高效模式
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {  //配置虚拟主机
        listen       80; //监听端口
        server_name  localhost;  //ip地址和域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
		
		// URL地址匹配是进行Nginx配置中最灵活的部分。 location支持正则表达式匹配,也支持条件判断匹配,用户可以通过location指令实现Nginx对动、静态网页进行过滤处理。使用location URL匹配配置还可以实现反向代理,用于实现PHP动态解析或者负载负载均衡。
        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;
        #}
    }

}

配置案例:
1.这样配置可达到,前端输入:localhost:9090,轮询访问 tomcat1、tomcat2,实现负载均衡

 #Tomcat 集群
    upstream  myapp {   #Tomcat 集群名称 
        server    localhost:8080;   #tomcat1配置
        server    localhost:8181 weight=2;   #tomcat2配置 ,weight 调整强度
    }
 
    server {
        listen       9090;
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://myapp;
            proxy_redirect default;
        }
     }

注:
在这里插入图片描述
配置案例:2.支持输入不同域名访问不同应用,实现反向代理

  upstream  tomcat1{   #Tomcat 集群名称 
        server    localhost:8080;   #tomcat1配置
    }
 
    server {
        listen       9090;
        server_name  www.sina.com;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://tomcat1;
            proxy_redirect default;
        }
     }



  upstream  tomcat2{   #Tomcat 集群名称
        server    localhost:8181;   #tomcat2配置
    }
 
    server {
        listen       9090;
        server_name  www.sohu.com; 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://tomcat2;
            proxy_redirect default;
        }
     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值