Nginx 配置反向代理和负载均衡,疯狂 java 讲义 pdf 百度云
? ?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 /模块名称/{
ssi on;
ssi_silent_errors on;
proxy_read_timeout 300;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://映射名称;
}
映射对应的具体服务地址
upstream 映射名称 {
server ip:端口;
}
负载均衡的实现就是写多个 server 服务就可以了,然后我们也可以对不同的服务设置不同的权重,这么就不进行过多的介绍了。这些都配置好之后,我们启动一下 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_user [request" '
? ? ? ? ? ? ? ? ?'body_bytes_sent "$http_referer" '
? ? ? ? ? ? ? ? ?'"http_x_forwarded_for"';
#access_log ?logs/access.log ?main;
sendfile ? ? ? ?on;
#tcp_nopush ? ? on;
#keepalive_timeout ?0;
keepalive_timeout ?65;
#gzip ?on;
upstream platform-activity {
server 127.0.0.1:8090;
}
upstream platform-admin {
server 127.0.0.1:8091; ?
}
upstream platform-es {
server 127.0.0.1:8092;?
}
upstream platform-goods {
server 127.0.0.1:8093;
}
upstream platform-im {
server 127.0.0.1:8094;?
}
upstream platform-log {
server 127.0.0.1:8095;
}
upstream platform-login { ?
server 127.0.0.1:8096; ??
}
upstream platform-mq {
server 127.0.0.1:8097; ??
}
upstream platform-score {
server 127.0.0.1:8098; ??
}
upstream platform-task {
server 127.0.0.1:8099; ??
}
upstream platform-team {
server 127.0.0.1:8190; ??
}
upstream platform-user {
server 127.0.0.1:8191; ??
}
server {
listen ? ? ? 80;
server_name ?192.168.10.112;
#charset koi8-r;
#access_log ?logs/host.access.log ?main;
location / {
root ? html;
index ?index.html index.htm;
}
location /platform-activity/{ ??
ssi on; ?
ssi_silent_errors on; ?
proxy_read_timeout 300; ?
proxy_set_header Host $host; ?
proxy_set_header X-Real-IP $remote_addr; ?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ?
proxy_pass http://platform-activity;
}
location /platform-admin/{ ??
ssi on; ?
ssi_silent_errors on; ?
proxy_read_timeout 300; ?
proxy_set_header Host $host; ?
proxy_set_header X-Real-IP $remote_addr; ?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ?
proxy_pass http://platform-admin;
}
location /platform-es/{ ??
ssi on; ?
ssi_silent_errors on; ?
proxy_read_timeout 300; ?
proxy_set_header Host $host; ?
proxy_set_header X-Real-IP $remote_addr; ?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ?
proxy_pass http://platform-es;
}
location /platform-goods/{ ??
ssi on; ?
ssi_silent_errors on; ?
proxy_read_timeout 300; ?
proxy_set_header Host $host; ?
proxy_set_header X-Real-IP $remote_addr; ?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ?
proxy_pass http://platform-goods;
}
location /platform-im/{ ??
ssi on; ?
ssi_silent_errors on; ?
proxy_read_timeout 300; ?
proxy_set_header Host $host; ?
proxy_set_header X-Real-IP $remote_addr; ?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ?
proxy_pass http://platform-im;
}
location /platform-log/{ ??
ssi on; ?
ssi_silent_errors on; ?
proxy_read_timeout 300; ?
proxy_set_header Host $host; ?
proxy_set_header X-Real-IP $remote_addr; ?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ?
proxy_pass http://platform-log;
}
location /platform-login/{ ??
ssi on; ?
ssi_silent_errors on; ?
proxy_read_timeout 300; ?
proxy_set_header Host $host; ?
proxy_set_header X-Real-IP $remote_addr; ?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ?
proxy_pass http://platform-login;
}
location /platform-mq/{ ??
ssi on; ?
ssi_silent_errors on; ?
proxy_read_timeout 300; ?
proxy_set_header Host $host; ?
proxy_set_header X-Real-IP $remote_addr; ?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ?
proxy_pass http://platform-mq;
}
location /platform-score/{ ??
ssi on; ?
ssi_silent_errors on; ?
proxy_read_timeout 300; ?
proxy_set_header Host $host; ?
proxy_set_header X-Real-IP $remote_addr; ?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ?
proxy_pass http://platform-score;
}
location /platform-task/{ ??
ssi on; ?
ssi_silent_errors on; ?
proxy_read_timeout 300; ?
proxy_set_header Host $host; ?
proxy_set_header X-Real-IP $remote_addr; ?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ?
proxy_pass http://platform-task;
}
location /platform-team/{ ??
ssi on; ?
ssi_silent_errors on; ?
proxy_read_timeout 300; ?
proxy_set_header Host $host; ?
proxy_set_header X-Real-IP $remote_addr; ?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ?
proxy_pass http://platform-team;
}
location /platform-user/{ ??
ssi on; ?
ssi_silent_errors on; ?
proxy_read_timeout 300; ?
proxy_set_header Host $host; ?
proxy_set_header X-Real-IP $remote_addr; ?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ?
proxy_pass http://platform-user;
}
#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 也是 window 的 nginx,但是 window 上面启动会出现很尴尬的事情,比如我双击 nginx.exe,然后一闪而过。。。没有任何其它的反应,一脸懵逼,到底是启动成功还是启动失败呢,这边我教大家一个鉴别的方法。
评论