首页 > 代码库 > nginx

nginx

nginx

  • nginx
  • 1.安装
    • 搭建nginx
  • 2.负载均衡配置
    • nginx配置文件


1.安装

搭建nginx

首先在本机下载好所需要的软件。
pcre http://www.pcre.org/
zlib http://www.zlib.net/
openssl http://www.openssl.org/source/
nginx http://nginx.org/en/download.html

 apt-get install build-essential #ubuntu默认不支持c/c++编译。添加支持
 apt-get install wget  gcc make #安装wget、gcc、make
 #设置一个新的目录为nginx存放相关
 mkdir -p /opt/local
 cd /opt/local
 #pcre
 tar -xvf /usr/download/pcre-8.36.tar.bz2
 cd pcre-8.36
 ./configure
 make
 make install
 #zlib
 tar -xvf /usr/download/nginx/zlib-1.2.8.tar.gz
 cd zlib-1.2.8/
 ./configure
 make
 make install
 #openssl
 tar -zxvf /usr/download/nginx/openssl-0.9.8zg.tar.gz
 cd openssl-0.9.8zg/
 ./config
 make
 make install
 #nginx
 tar -zxvf /usr/download/nginx/nginx-1.9.4.tar.gz
 cd nginx-1.9.4.tar.gz
 ./configure
 make
 make install

nginx安装好的目录在/usr/local/nginx

如果安装完 运行/usr/local/nginx/sbin/nginx出现:

 error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

解决办法是:

 32位系统
 #  ln -s /usr/local/lib/libpcre.so.1 /lib
 64位系统
 #  ln -s /usr/local/lib/libpcre.so.1 /lib64

启动成功后 就可以尝试访问

2.负载均衡配置

在/usr/local/nginx/config中

nginx配置文件

 #以什么用户启动,推荐root.否则会出现图片大于65k无法访问的问题
 #user  nobody;
 #工作子进程数量 =cpu数量 or 2*cpu
 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 {
 #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
 use epoll;
 #允许最大连接数
 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;
 upstream localhost{
 #weight 权重 值越大 被分配几率越大
 server localhost:18080 weight=1;
 server localhost:18082 weight=1;
 }
 server {
 listen       80;
 server_name  localhost;
 #charset koi8-r;
 #access_log  logs/host.access.log  main;
 location / {
 #root   html;
 #index  index.html index.htm;
 #请求转向真实地址 localhost为上面 upstream localhost的名字
 proxy_pass http://localhost;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
 #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;
 #    }
 #}
#  }

修改配置之后

ps -ef |grep nginx
 #kill -9
 #nginx -s stop
 nginx -s reload #重启
 #然后重启

配置tomcat。配置不同的tomcat端口。上面配置的是两个tomcat 18080 18082
启动tocmat。
访问localhost.可以看到,请求分别分发到不同的tomcat中。

nginx