1、描述
本人主要对nginx 简单的使用,对于一些简单的操作是可以满足的,如果要复杂的功能,后续可以补充。
2、环境
(1)window 环境
(2)springboot
(3)jdk8 或者 jdk 11
(4)nginx-1.12.2
(5)window 的hosts文件配置:127.0.0.1 nginx.com
3、代码链接
里面包括nginx-1.12.2 和简单实现代码的springboot:
https://download.csdn.net/download/wcy18818429914/15545326
4、Nginx (如果想测试可以直接跳过该步骤,代码已经帮大家配置好了)
(1)将nginx-1.12.2 解压放到某个目录下就可以
(2)启动nginx :
1)第一种:直接双击nginx.exe,双击后一个黑色的弹窗一闪而过
2)第二种:打开cmd命令窗口,切换到nginx解压目录下,输入命令 nginx.exe 或者 start nginx ,回车即可
(3)检查nginx是否启动成功
输入网址 http://localhost:80,回车,出现以下页面说明启动成功
(4)nginx 的配置信息:
conf目录下的nginx.conf是nginx的配置文件,默认配置的nginx监听的端口为80,如果80端口被占用可以修改为未被占用的端口即可
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80; #监听80也就是监听127.0.0.1/的请求
server_name localhost; #监听的ip地址或者域名,可以通过正则匹配
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
########default 把这里的conf也包含进来#######
include conf_d/*.conf;
}
配置反向代理的程序:nginx.conf:
location 是指定转发对象:
我们可以修改nginx的配置文件nginx.conf达到访问nginx代理服务器时跳转到指定服务器的目的,既通过proxy_pass 配置请求转发地址,即当我们依然输入http://http:// nginx.com/xxxx时,请求会跳转到我们配置的服务器
1)location / :代表输入:http:// nginx.com 回到 index
2)location /nginx1 :代表 域名后面带有 nginx1时,输入:http:// nginx.com/nginx1 转发到 http://127.0.0.1:8091 这个地址上
2)location /nginx2 :代表 域名后面带有 nginx1时,输入:http:// nginx.com/nginx2 转发到 http://127.0.0.1:8092 这个地址上
这样就实现了各个项目不同,可以共用一个域名,达到反向代理的功能,如果还有多个项目可以继续在这里面添加信息
server {
listen 80; #监控端口
server_name nginx.com;# 转发的域名
location / {
index index;
include proxy.conf;
}
location /nginx1 {
proxy_pass http://127.0.0.1:8091; #第一个项目转发
include proxy.conf;
}
location /nginx2 {
proxy_pass http://127.0.0.1:8092;#第二个项目转发
include proxy.conf;
}
}
(5).关闭nginx
如果使用cmd命令窗口启动nginx,关闭cmd窗口是不能结束nginx进程的,可使用两种方法关闭nginx
(1)输入nginx命令 nginx -s stop(快速停止nginx) 或 nginx -s quit(完整有序的停止nginx)
(2)使用taskkill taskkill /f /t /im nginx.exe
5、springboot 简单启动服务
启动里面nginx 1 和nginx 2 的 服务器
6、测试是否成功
(1)首先确定本地是否配置好hosts: 127.0.0.1 nginx.com
(2)访问第一个代理:http://nginx.com/nginx1/test ,有以下就是正确的:
(3)访问第一个代理:http://nginx.com/nginx2/test ,有以下就是正确的: