今天做了用nginx负载均衡一个项目,搭建了两台服务器!
nginx.conf配置文件如下:
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream AK8{
server 10.1.254.40:8026 weight=1;
server 10.1.254.41:8026 weight=1;
}
server {
listen 80;
server_name www.lining.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#X-Frame-Options是为了防止clickJacking攻击
add_header X-Frame-Options SAMEORIGIN;
#在某些浏览器上禁用内容类型嗅探。
add_header X-Content-Type-Options nosniff;
#此标头启用跨站点脚本(XSS)过滤器
add_header "X-XSS-Protection: 1; mode=block");
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_pass http://AK8;
#缓冲区溢出攻击 是通过将数据写入缓冲区并超出缓冲区边界和重写内存片段来实现的,限制缓冲区大小可有效防止
#默认8k或16k,表示客户端请求body占用缓冲区大小。如果连接请求超过缓存区指定的值,那么这些请求实体的整体或部分将尝试写入一个临时文件。
client_body_buffer_size 1048M;
#表示客户端请求的最大可接受body大小,它出现在请求头部的Content-Length字段, 如果请求大于指定的值,客户端将收到一个"Request Entity Too Large" (413)错误,通常在上传文件到服务器时会受到限制
client_max_body_size 20480M;
#设置客户端连接保持会话的超时时间,超过这个时间,服务器会关闭该连接。
keepalive_timeout 60;
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
没毛病,项目起来了,在谷歌浏览器上访问nginx地址也访问到服务了!但是由于我们做的程序有的功能是需要用IE浏览器去访问的,就在我用IE浏览器去访问的时候出现了个问题,页面上的图片加载不出来,于是开始百度:有说是权限问题,需要将nginx第一行:#user nobody;改成:user root; 静态文件夹权限打开:chmod 777 ./www ,好,改完一试还是不好使,其实也想到不是权限的问题,因为谷歌浏览器就能访问到,那这就奇怪了呀,这在咋整?继续查百度,又看到改配置文件如下:
user root;
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 AK8{
server 10.1.254.40:8095 weight=1;
server 10.1.254.41:8095 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /{
#X-Frame-Options是为了防⽌clickJacking攻击
add_header X-Frame-Options SAMEORIGIN;
#在某些浏览器上禁⽤内容类型嗅探。
add_header X-Content-Type-Options nosniff;
#此标头启⽤跨站点脚本(XSS)过滤器
add_header "X-XSS-Protection: 1; mode=block");
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_pass http://AK8;
#缓冲区溢出攻击 是通过将数据写⼊缓冲区并超出缓冲区边界和重写内存⽚段来实现的,限制缓冲区⼤⼩可有效防⽌
#默认8k或16k,表⽰客户端请求body占⽤缓冲区⼤⼩。如果连接请求超过缓存区指定的值,那么这些请求实体的整体或部分将尝试写⼊⼀个临时⽂件。
client_body_buffer_size 1048M;
#表⽰客户端请求的最大可接受bodySize,它出现在请求头部的Content-Length字段, 如果请求⼤于指定的值,客户端将收到⼀个"Request Entity Too Large" (413)错误
client_max_body_size 20480M;
#设置客户端连接保持会话的超时时间,超过这个时间,服务器会关闭该连接。
keepalive_timeout 60;
root html;
index index.html index.htm;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
proxy_pass http://AK8;
}
location ~ .*\.(js|css)?$
{
proxy_pass http://AK8;
}
#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.conf配置文件对静态文件加访问路径,加上,重启nginx,两台服务,还是不好使!!!哦,No!
然后就是经验之谈了,公司大佬告知改一下nginx版本:改用1.16.0版本的,刚用的都是1.18.0版本的。
改完版本后,重启nginx和服务,就好使了!问了一下大佬这是什么原因?大佬告诉我:“可能一些默认配置项不一样了吧!”
哦,有道理,你不会还要问那些默认配置不一样吧?别问,问我也不知道,想知道,自己去百度吧!