今天给大家介绍一下如何利用Nginx进行反向代理,之所以介绍这个的原因是,因为开发的时候遇到一个很尴尬的场景。因为是springboot项目,所以每一个控制类的端口都不一样,但是app那边所有接口都是对应一个ip和一个端口。如果我们想要实现本地app调式,就必须配置一个nginx,进行反向代理连接我们启动的服务器。废话不多说,开始我们nginx配置的介绍。
首先我们需要下载一个nginx,大家可以去官网上面下载,也可以直接在我的百度云盘下载。(nginx版本nginx-1.13.2)云盘地址:链接:https://pan.baidu.com/s/1NwZvS6-9cq5egCxsOHyrYg 密码:tw4p
然后接下来主要工作是在nginx中修改nginx.conf配置就可以了。
nginx.conf模块配置文件如下所示,这边我们只需要修改一些配置,就可以简单的实现反向代理和负载均衡功能了,我们先来看一下默认的配置文件。
#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_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;
server { listen 80; server_name localhost;
#charset koi8-r;
#access_log logs/host.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; }
# 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 /模块名称/{
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_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 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,然后一闪而过。。。没有任何其它的反应,一脸懵逼,到底是启动成功还是启动失败呢,这边我教大家一个鉴别的方法。
首先我们通过start nginx命令启动一下nginx,命令输入后的效果如下所示(其实就是没有任何效果):
然后我们可以输入tasklist /fi "imagename eq nginx.exe"命令,来查看nginx.exe的进程是否启动
如果启动成功就会出现相关进程的打印信息,我们也可以通过nginx -s stop命令来停止服务,因为如果nginx服务启动的话,停止命令不会出现问题,如果服务未启动的话,那命令就会报错。
nginx服务启动
nignx服务未启动
然后我们接着来看服务重启的命令,也就是nginx -s reload
那如果服务启动有问题,我们应该如何来查看错误信息呢,我们可以在logs日志文件夹下面查看error.log的报错信息,所有nginx错误都会写在这个日志下面。
这边在给大家额外说一下我们遇到过的错误,最经常的就是nginx启动的默认端口被占用,导致启动出错。我们可以用netstat -ano | findstr 80 命令来查看这个端口被什么暂用,如果暂用的不是重要的进程,我们就可以通过taskkill /F /PID 1234来杀死这个进程,如果是系统占用的话,我们只能修改nginx默认的启动端口了。总结:
nginx搭建和配置的过程中还是遇到过蛮多问题的,一个是端口启动被占用问题,一个是nginx启动不知道怎么去确定nginx是否启动问题。最后一个就是window下面的nginx命令不是很熟悉。好在问题都已经解决了,总之最可怕的不是问题,是有问题你还不知道。