Nginx配置文件
- /usr/local/nainx/conf/nginx.conf
文件结构
1、 全局块
#配置影响全局的配置
#用户组
#user nobody;
#允许进程数
worker_processes 1;
#全局错误日志 notice、info 日志级别
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid 存放路径
#pid logs/nginx.pid
2 、 events块
events {
#最大连接数
worker_connections 1024;
}
3 、 http块
- 嵌套多个serve
- 配置反向代理功能提供负载均衡支持
http {
#多个主机对应多个server
...
server{
#虚拟主机配置;web服务器
...
location [pattern]{
#正则过来url,不同url对应不同location
...
}
...
location[pattern]{
...
}
...
}
server{
...
}
}
4 、 server块
- 配置虚拟主机的相关参数
5 、 location块
- 请求URL过滤,正则匹配
配置后nginx.conf
#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;
# 与location块中proxy_pass的名字相同
upstream itripbiz_server{
server 127.0.0.1:8080;
}
#第一个server
server {
#监听端口
listen 80;
#监听地址,使用该地址访问
server_name itrip.project.bdqn.cn;
#location 中提出设置默认路径默认页
root /data/itrip/itripfront;
index index.html;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#将request请求中的post和客户端真实ip的代理到tomcat,否则只能取到nginx ip
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
#反向代理
proxy_pass http://itripbiz_server;
}
#静态文件缓存
location ~ .*\.(jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$
{
proxy_set_header Host $host;
proxy_pass http://itripbiz_server;
expires 6h;
}
location ~ .*\.(js|css)?$
{
proxy_set_header Host $host;
proxy_pass http://itripbiz_server;
expires 2h;
}
access_log /data/logs/nginx/app_access.log;
#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;
# }
#}
反向代理成功设置成功,启动tomcat
检测nginx.conf是否合法
/usr/local/nginx/sbin/nginx -t
出现错误
[root@MiWiFi-R4-srv bin]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] "upstream" directive is not allowed here in /usr/local/nginx/conf/nginx.conf:45
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
# upstream 位置不对放在server 块外
[root@MiWiFi-R4-srv bin]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] directive "location" has no opening "{" in /usr/local/nginx/conf/nginx.conf:57
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@MiWiFi-R4-srv bin]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [emerg] open() "/data/logs/nginx/app_access.log" failed (2: No such file or directory)
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
#文件路径不存在,mkdir -p ........
[root@MiWiFi-R4-srv bin]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#检测通过
#启动nginx
[root@MiWiFi-R4-srv bin]# /usr/local/nginx/sbin/nginx
配置负载均衡
1、轮询
upstream itripbiz_server
{#轮询方式
server 127.0.0.1:8082;
server 127.0.0.1:8080;
}
2、热备
- backup
upstream itripbiz_server
{#热备方式 宕机后转发至热备服务器
server 127.0.0.1:8082;
server 127.0.0.1:8080 backup;
}
3、权重
- weight
upstream itripbiz_server
{#权重
server 127.0.0.1:8082;
server 127.0.0.1:8080 weigth=2;
}
4、IP地址hash
- ip_hash
- 解决Tomcat之间session 共享
upstream itripbiz_server
{ ip_hash;
server 127.0.0.1:8082;
server 127.0.0.1:8080 weigth=2;
}