Nginx
大约 2 分钟
What's Nginx?
相关信息
Nginx 是一个实现反向代理的轻量级服务器。可以部署前端和代理后端。
当一个项目同时部署在多个服务器上,而用户在访问时都访问一个地址时,就需要使用反向代理将用户的请求根据负载均衡策略转发到各个服务器上。
配置文件
#user nobody;
# 进程数,常与cpu核数一致
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;
# 配置集群
# 此处名字不能带下划线,tomcat9.0开始不支持,运行时出错
upstream tomcatpool{
server [ip]:[端口号] weight=5;
server [ip]:[端口号] weight=5;
}
#gzip on;
# 一个server相当于一个代理服务器的配置
server {
# 反向代理服务器nginx的端口
listen 8090;
# 反向代理服务器名,建议用本机ip
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# 用来过滤请求,一个location就是一个规则
# 配置nginx代理的真实服务器,以及静态资源路径
location / {
root html;
index index.html index.htm;
# 配置真实服务器(tomcat...)
# proxy_pass http://[ip]:[端口];
# 配置集群,设置代理服务器
# proxy_pass http://tomcat pool;
}
# 用来过滤静态请求,一个location就是一个规则
# 配置nginx代理的真实服务器,以及静态资源路径
location ~*\.(jpg|css|js|png|ioc)$ {
root html;
index index.html index.htm;
# 配置真实服务器(tomcat...)
# proxy_pass http://[ip]:[端口];
}
#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 [ip]:[端口]
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# 配置静态资源的路径
# root d:\static
# 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;
# }
#}
}