系列文章目录
前言
一、nginx学习大纲?
1、nginx.基本描述是什么、做什么、用在哪里
2、安装与配置文件,常用命令
3、nginx反向代理,正向代理
4、负载均衡
5、动静分离
6、高可用服务器
7、执行原理
二、详细内容
nginx配置如下:
#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;
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;
# }
#}
}
安装依赖
pcre nginx zlib ssl
nginx安装路径
/usr/local/nginx/sbin/
常用命令:
nginx -v
nginx -s stop start reload
配置文件位置:
/usr/local/nginx/conf
包含三部分
1、全局块
2、event块
3、http块(http server)
反向代理事例:
nginx反向代理 httpserver或者tomacat等
域名配置方法:
windows 配置域名访问如路径C:\Windows\System32\drivers\etc或者window32/etc等
这里配置的127.0.0.1 www.hao123.com
反向代理实例:
反向代理1 通过/ 转发到8080服务上
location ~/{
proxy_pass 127.0.0.1:8080
}
反向代理2 通过路径转发到指定端口去
localtion 中,配置
location ~/edu/{
proxy_pass 127.0.0.1:8001
}
负载均衡实例:
请求转发到不同的服务上8001-8002上
在http的块里添加
upstream myserver{
server 192.168.17.129:8001;
server 192.168.17.129:8002;
}
proxy_pass改为定义的http://myserver;
负载均衡方法:
轮询默认 weight(权重越高,被分配客户端越多)
ip_hash(新增字段ip_hash 用户指定某一台服务器)
fair(第三方新增fair根据相应时间)
nginx动静分离:
动态请求跟静态请求分开
1. nginx处理静态页面 静态资源服务区 htlm css image
2.tomcat处理动态请求 取数据库内容
location ~/image/{
root /www
autoindex on;
}
高可用服务器:
主服务器宕机后,怎么处理,如何切换
两台nginx+keepalive,即两台ip port,需要对外提供一个ip port,虚拟ip服务器
环境准备,两台服务器192.168.17.129-131
静态服务器安装nginx软件与keepalive
yum install keepalive y
/etc/keepalive.conf
三、提高篇
1、本身只支持http服务,如何通过https转发到http服务上
a)nginx配置如下
location ^~ /https_proxy/ {
if ( $request_uri ~ /https_proxy/(.*)){
set $rawurl $1;
}
proxy_pass http://$rawurl;
}
b)url拼接
1、自己服务的域名(或者外网https ip+port)+/https_proxy/+ httpurl(去掉http://)
2、对方服务的域名(或者外网https ip+port)+/https_proxy/+ httpurl(去掉http://) 注意需要他们nginx上配置转发如(a)
例子如下:
http://127.0.0.1:8110/vevent/eventId
https://12.12.12.17:444/https_proxy/1127.0.0.1:8110/vevent/eventId
总结
通过本文的学习,对nginx本身应该没有任何疑问了把。