nginx安装(centos和ubuntu)

centos安装nginx

安装依赖库

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

安装nginx

./configure --prefix=/usr/local/nginx
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module(安装ssl模块)
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-openssl=/usr/local/openssl-1.0.2m --with-http_ssl_module --with-pcre=/usr/local/pcre-8.41 --with-zlib=/usr/local/zlib-1.2.11
make
make install

问题

安装nginx的时候遇到如下问题

Configuration summary  
  + using system PCRE library  
  + OpenSSL library is not used  
  + using builtin md5 code  
  + sha1 library is not found  
  + using system zlib library 

出现以上问题的原因是,在安装nginx的时候没有指定openssl的解压路径。正确的做法如下:

./configure --prefix=/usr/local/nginx  
--with-openssl=/usr/local/openssl-1.1.0g
--with-http_ssl_module

同理,如果pcre和zlib出现类似的问题,指定路径就可:

--with-pcre=/usr/local/pcre-8.41 
--with-zlib=/usr/local/zlib-1.2.11 
--with-http_stub_status_module

ubuntu安装nginx

安装依赖库

安装gcc g++的依赖库

apt-get install build-essential
apt-get install libtool

安装 pcre依赖库

sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev

安装 zlib依赖库

apt-get install zlib1g-dev

安装 ssl依赖库

apt-get install openssl

安装nginx

./configure --prefix=/usr/local/nginx
make
sudo make install

常用命令

启动nginx

./nginx

关闭nginx

./nginx -s stop
./nginx -s quit

重启nginx

./nginx -s reopen

重新加载配置

./nginx -s reload

查看版本

./nginx -v

配置

在conf/nginx.conf中配置


#user  nobody;#指定nginx worker进程运行用户以及用户组,默认nobody账号运行
worker_processes  1;#nginx要开启的子进程数量,运行过程中监控每个进程消耗内存(一般几M~几十M不等)根据实际情况进行调整,通常数量是CPU内核数量的整数倍

#error_log  logs/error.log; #定义错误日志文件的位置及输出级别【debug / info / notice / warn / error / crit】 
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;#进程pid存放位置

#nginx工作模式配置
events {
    worker_connections  1024;#单个后台worker process进程的最大并发链接数
}

#http模块
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;#激活tcp_nopush参数可以允许把httpresponse header和文件的开始放在一个文件里发布。积极的作用是减少网络报文段的数量

    #keepalive_timeout  0;#连接超时时间,单位是秒
    keepalive_timeout  65;

    #gzip  on;#开启gzip压缩功能

	#server模块
	#基于域名的虚拟主机
    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;
    #    }
    #}

}

负载均衡测试

建立springboot的web项目testnginx:

在这里插入图片描述

在这里插入图片描述

打成jar包,上传到Linux,放在3个文件夹下

在这里插入图片描述

通过启动参数指定端口号–server.port=8081等

在这里插入图片描述

启动3个程序
在这里插入图片描述

用浏览器访问
在这里插入图片描述

配置负载均衡

http{
	……
	#配置负载均衡服务器列表
	upstream localweb{
		server localhost:8081;
		server localhost:8082;
		server localhost:8083;
	}
	server {
		listen       80;
		server_name  192.168.1.101;
		 location / {
		 	proxy_pass http://localweb;#请求转向localweb定义的服务器列表
		 	#以下是一些反向代理的配置(可选择性配置)
		 	#proxy_redirect off;
		 	proxy_set_header Host $host;#$host是指请求主机头字段,否则为服务器名称。
		 	proxy_set_header X-Real-IP $remote_addr;#$remote_addr是指客户端的IP地址
		 	#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
		 	proxy_set_header X-Forwarded-For $remote_addr;
		 	proxy_connect_timeout 90;          #nginx跟后端服务器连接超时时间(代理连接超时)
		 	proxy_send_timeout 90;             #后端服务器数据回传时间(代理发送超时)
		 	proxy_read_timeout 90;             #连接成功后,后端服务器响应时间(代理接收超时)
		 	proxy_buffer_size 4k;              #设置代理服务器(nginx)保存用户头信息的缓冲区大小
		 	proxy_buffers 4 32k;               #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
		 	proxy_busy_buffers_size 64k;       #高负荷下缓冲大小(proxy_buffers*2)
		 	proxy_temp_file_write_size 64k;    #设定缓存文件夹大小,大于这个值,将从upstream服务器传
		 	client_max_body_size 10m;          #允许客户端请求的最大单文件字节数
		 	client_body_buffer_size 128k;      #缓冲区代理缓冲用户端请求的最大字节数
		 }
	}
	……
}

访问http://192.168.1.101/go
在这里插入图片描述

可以看出,默认负载均衡算法是轮询。
或者配置为其他算法

	upstream localweb{
		ip_hash; #每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
		server localhost:8081;
		server localhost:8082;
		server localhost:8083;
	}
	upstream localweb{
	#weigth参数表示权值,权值越高被分配到的几率越大
		server localhost:8081 weight=5;
		server localhost:8082 weight=2;
		server localhost:8083 weight=1;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

下一页天空

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值