docker run --name nginx -d -p 80:80 -v /nginx/html:/usr/share/nginx/html -v /nginx/nginx.conf:/etc/nginx/nginx.conf nginx
下面为容器的端口!!!!!!
# 运行用户
#user nobody;
# 启动进程,通常设置成和cpu数量相等
worker_processes 1;
#全局错误日志及PID文件
#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; #单个后台worker process进程的最大并发链接数
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型,类型由mime.type文件定义
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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
#必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile on;
#将tcp_nopush和tcp_nodelay两个指令设置为on用于防止网络阻塞
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#开启gzip压缩
#gzip on;
server {
#表示当前的代理服务器监听的端口
listen 80;
#表示监听到之后需要转到哪里去,这时我们直接转到本地,这时是直接到nginx文件夹内
server_name localhost;
#charset koi8-r;
#设定本虚拟主机的访问日志
#access_log logs/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;
}
#PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
# 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
#
#禁止访问 .htxxx 文件
#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;
# }
#}
server {
listen 80;
server_name wiki.zhoan.net;
location / {
proxy_pass http://47.106.95.75:10085/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name zentao.zhoan.net;
location / {
proxy_pass http://47.106.95.75:10086;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name jenkins.zhoan.net;
location / {
proxy_pass http://47.106.125.91:32772/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name gitlab.zhoan.net;
location / {
proxy_pass http://47.106.125.91/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name kanfangzhu.zhoan.net;
location / {
proxy_pass http://111.230.249.181:9000/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#include C:\\Users\\Administrator\\Desktop\\nginx-1.14.0\\conf\\confsite\\mysite.conf;
}
二、vv
#
#定义Nginx运行的用户和用户组
user vv vv;
#nginx进程数,建议设置为等于CPU总核心数。
worker_processes 1;
#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid进程文件路径
pid logs/nginx.pid;
events {
#启用epoll
use epoll;
#连接数,指定进程可以打开的最大描述符:数目
worker_connections 1024;
}
http {
#文件扩展名与文件类型映射表
include mime.types;
#默认文件类型
default_type application/octet-stream;
#nginx日志格式
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;
#隐藏nginx版本号
server_tokens off;
#开启高效传输模式
sendfile on;
#防止网络阻塞
tcp_nodelay on;
tcp_nopush on;
#keepalive长连接超时时间
keepalive_timeout 60;
#客户端请求头部的缓冲区大小
client_header_buffer_size 4k;
#gzip压缩功能设置
#开启gzip压缩输出
gzip on;
#最小压缩文件大小
gzip_min_length 1k;
#压缩缓冲区
gzip_buffers 4 16k;
#压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
gzip_http_version 1.1;
#压缩等级,压缩等级 1-9 等级越高,压缩效果越好,节约宽带,但CPU消耗大
gzip_comp_level 2;
#压缩类型
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
# http_proxy 设置
#允许客户端请求的最大单文件字节数
client_max_body_size 10m;
#缓冲区代理缓冲用户端请求的最大字节数
client_body_buffer_size 128k;
#nginx跟后端服务器连接超时时间(代理连接超时)
proxy_connect_timeout 300;
#后端服务器数据回传时间(代理发送超时)
proxy_send_timeout 300;
#连接成功后,后端服务器响应时间(代理接收超时)
proxy_read_timeout 300;
#设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffer_size 64k;
#proxy_buffers缓冲区
proxy_buffers 4 512k;
#高负荷下缓冲大小(proxy_buffers*2)
proxy_busy_buffers_size 512k;
#设定缓存文件夹大小,大于这个值,将从upstream服务器传
proxy_temp_file_write_size 512k;
#反向代理缓存目录,#设置内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=cache:200m inactive=1d max_size=30g;
#指定临时缓存文件的存储路径(路径需和上面路径在同一分区)
proxy_temp_path /usr/local/nginx/proxy_temp 1 2;
#
proxy_cache_key "$scheme$request_method$host$request_uri$is_args$args";
#设置状态码为200和304的响应可以进行缓存,并且缓存时间为10分钟
proxy_cache_valid 200 302 10m;
#设置状态码为404的响应可以进行缓存,并且缓存时间为1分钟
proxy_cache_valid 404 1m;
#这是每个请求能用磁盘上临时文件最大大小。这些当上游响应太大不能装配到缓冲区时被创建。
proxy_max_temp_file_size 2048m;
#防止出现499状态码,让服务器不主动关闭客户端连接
proxy_ignore_client_abort on;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
#设定负载均衡后台服务器列表
upstream name {
#每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
#ip_hash;
server 192.168.2.224:8080;
server 192.168.2.148:8080;
}
server {
#监听端口
listen 80;
#域名可以有多个,用空格隔开
server_name localhost;
root /www;
#默认编码
charset utf-8;
#访问日志文件存放目录
access_log logs/host.access.log;
location / {
proxy_pass http://name;
root /www;
index index.jsp index.html index.htm;
}
#静态文件,nginx自己处理,不去请求tomcat
location ~ .*/*.(gif|jpg|jpeg|png|bmp|swf)$
{
root /www;
#proxy_pass http://name;
expires 30d;
}
#设定查看Nginx状态的地址
location /nginx_status {
stub_status on;
access_log off;
}
location ~ ^/(WEB-INF)/ {
deny all;
}
#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;
# }
#}
}
location [=|~|~*|^~] /uri/ { … }
符号 | 含义 |
---|---|
= | 开头表示精确匹配 |
^~ | 开头表示 uri 以某个常规字符串开头,理解为匹配 url 路径即可。nginx 不对 url 做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格) |
~ | 开头表示区分大小写的正则匹配 |
~* | 开头表示不区分大小写的正则匹配 |
/ | 通用匹配,任何请求都会匹配到 |
多个 location 配置的情况下匹配顺序为
=
^~
location = / {
#规则A
}
location = /login {
#规则B
}
location ^~ /static/ {
#规则C
}
location ~ \.(gif|jpg|png|js|css)$ {
#规则D
}
location ~* \.png$ {
#规则E
}
location / {
#规则F
}
那么产生的效果如下:
访问根目录 /, 比如 http://localhost/ 将匹配规则 A
访问 http://localhost/login 将匹配规则 B,http://localhost/register 则匹配规则 F
访问 http://localhost/static/a.html 将匹配规则 C
访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则 D和规则 E,但是规则 D 顺序优先,规则 E不起作用,而 http://localhost/static/c.png则优先匹配到规则 C
访问 http://localhost/a.PNG 则匹配规则 E,而不会匹配规则 D,因为规则 E 不区分大小写
访问 http://localhost/category/id/1111 则最终匹配到规则 F,因为以上规则都不匹配,这个时候应该是 nginx 转发请求给后端应用服务器,比如 FastCGI(PHP),tomcat(jsp),nginx 作为反向代理服务器存在
实际使用中,至少有三个匹配规则定义,如下:
# 直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
# 这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / {
proxy_pass http://tomcat:8080/index
}
# 第二个必选规则是处理静态文件请求,这是 nginx 作为 http 服务器的强项
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}
# 第三个规则就是通用规则,用来转发动态请求到后端应用服务器
# 非静态文件请求就默认是动态请求,自己根据实际把握
# 毕竟目前的一些框架的流行,带.php、.jsp后缀的情况很少了
location / {
proxy_pass http://tomcat:8080/
}