Nginx的作用
Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理。
Nginx提供的负载均衡策略有2种:内置策略和扩展策略。内置策略为轮询,加权轮询,Ip hash。
Docker部署Web应用
1 部署nginx
docker run -it --rm -d -p 8090:80 --name web1 nginx
2 访问部署的nginx应用
curl localhost:8090
3、部署Tomcat
docker run -d -p 8888:8080 --name web2 tomcat
4、访问部署的Tomcat
curl localhost:8888
因为默认镜像webapps目录为空,所以直接访问提示:http status 404
5、404处理(进入容器,复制webapps.dist 到 webwpps)
docker exec -it web2 /bin/bash
cp -r webapps.dist/* webapps
6、访问修改后的Tomcat
本机安装并配置Nginx代理
nginx安装
centos安装参考链接:
Nginx 安装
/usr/local/webserver/nginx/sbin/nginx -v
修改nginx.conf
vi /usr/local/webserver/nginx/conf/nginx.conf
#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;
}
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 minio-server {
# weight:默认为1。weight越大,负载的权重就越大。
# backup:其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
# max_fails:允许请求失败的次数默认为1,当超过最大次数时,返回 proxy_next_upstream 模块定义的错误。
# fail_timeout:Nginx基于连接探测,如果发现后端异常,在单位周期为fail_timeout设置的时间中达到max_fails次数,这个周期次数内,如果后端同一个节点不可用,那么把节点标记为不可用,并等待下一个周期(同样时常为fail_timeout)再一次去请求,
判断是否连接是否成功。
server 127.0.0.1:8888 weight=2 max_fails=3 fail_timeout=10s;
server 127.0.0.1:8090 weight=1 fail_timeout=30s;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
# 传输文件缓存大小及单次请求大小
client_body_buffer_size 10M;
client_max_body_size 1G;
# 宕机检测,如果设置时间内无响应,则直接切换到其它服务
proxy_connect_timeout 4;
proxy_send_timeout 4;
proxy_read_timeout 4;
proxy_pass http://minio-server;
}
#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;
# }
#}
测试Nginx代理
更多参考
Nginx 安装配置
Nginx - 学相伴
nginx配置实例及多服务器负载