liunx 客户端putty登录,cd 到/usr/local 目录下
cd /usr/local #进入到/usr/local目录下
mkdir nginx #创建nginx 目录
到nginx 官网下载nginx安装包
官网地址:http://nginx.org/en/download.html
下载后用ftp把安装包上传到nginx目录下
查看是否上传成功
cd /usr/local/nginx
ll
nginx是C语言开发,建议在linux上运行,本教程使用Centos7作为安装环境。
n gcc
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc-c++
n PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括perl兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
n zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel
n openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel
执行nginx 安装命令
tar -zxvf nginx-1.8.1.tar.gz
进行configure
./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
注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
cd /varmkdir temp #创建temp目录
cd ./temp #进入temp
mkdir nginx #创建nginx目录
cd /usr/local/nginx/nginx-1.8.1
make
make install
nginx 的启动和关闭
启动:
进入nginx的sbin目录
cd /usr/local/nginx/sbin
./nginx #启动nginx
浏览器访问:(如果访问不了请看下liunx的防火墙是否关闭)
关闭nginx :
可以使用kill命令,不推荐使用
推荐使用:./nginx -s stop
刷新配置:./nginx -s reload
Nginx的配置:
在/usr/local/nginx/conf目录下nginx.conf文件是nginx的配置文件。
nginx 配置负载均衡:
vi /usr/local/nginx/conf/nginx.conf #打开nginx 配置文件
在http 和server 之间添加:
upstream testA {
#后台
server localhost:9081;
#多个服务器配置负载均衡
server localhost:9082;
}
在server 里面的 location / {} 把root 和 index 注释掉,添加:
proxy_pass http://testA;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Scheme $scheme;
proxy_connect_timeout 1;
proxy_read_timeout 30;
proxy_send_timeout 60;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
access_log on;
break;
保存退出
cd /usr/local/nginx/sbin #进入nginx sbin 目录
./nginx -s stop #停止nginx
./nginx -s reload #刷新nginx 配置文件
注:如果报错(nginx: [error] open() "/var/run/nginx/nginx.pid" failed (2: No such file or),解决办法
##创建目录即可
mkdir /var/run/nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
重新刷新nginx配置文件
./nginx -s reload #刷新nginx 配置文件
./nginx #启动nginx
浏览器输入192.168.133.128刷新几次,可以看见每次调用的tomcat不一样:注(我的ip是192.168.133.128,自己的ip请自己查看)
nginx的五种负载算法模式:http://blog.csdn.net/gzh0222/article/details/8095994
nginx 的nginx.con 配置文件内容:
#user nobody;
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
worker_rlimit_nofile 10240;
#pid logs/nginx.pid;
events {
worker_connections 10240;
}
http {
server_names_hash_bucket_size 64;
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;
server_tokens off;
client_max_body_size 100m;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
client_header_buffer_size 4k;
open_file_cache max=10240 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 1;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6]\.";
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
upstream testA {
#后台
server localhost:9081;
server localhost:9082;
server localhost:9083;
server localhost:9084;
}
server {
listen 80;
server_name localhots;
#charset koi8-r;
#access_log logs/www/host.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://testA;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Scheme $scheme;
proxy_connect_timeout 1;
proxy_read_timeout 30;
proxy_send_timeout 60;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
access_log on;
break;
}
#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;
# }
#}
}