实现Nginx的反向代理作用
什么是反向代理:反向代理服务器对外的表现是Web服务器,其主要的技术就是地址转换。反向代理技术中每个Web服务器现代感与反向代理服务器的某个目录。反向代理可以把服务器的目录映射在需要进行代理的服务器上。
来源:百度百科
实验环境:centos7、搭好tomcat服务(可使用域名加端口号/ip加端口访问我们的tomcat页面)、防火墙关闭
实验目的:部署Nginx反向代理使用域名访问Tomcat的8080系统(不加端口可直接访问)
我们已经搭建好Tomcat服务,浏览器也可以加端口访问我们的界面
安装Nginx
- 安装前我们可以使用whereis nginx查看服务器上是否已经安装了。
- 安装前准备:c++语言环境,以及openssl、pcre、zlib组件(这里已经安装成功)
[root@qmfz nginx-1.14.2]# yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.njupt.edu.cn
* extras: mirrors.ustc.edu.cn
* updates: mirrors.ustc.edu.cn
软件包 gcc-4.8.5-44.el7.x86_64 已安装并且是最新版本
软件包 zlib-1.2.7-18.el7.x86_64 已安装并且是最新版本
软件包 zlib-devel-1.2.7-18.el7.x86_64 已安装并且是最新版本
软件包 pcre-devel-8.32-17.el7.x86_64 已安装并且是最新版本
软件包 1:openssl-1.0.2k-21.el7_9.x86_64 已安装并且是最新版本
软件包 1:openssl-devel-1.0.2k-21.el7_9.x86_64 已安装并且是最新版本
无须任何处理
- nginx默认是安装在/usr/local目录下的,所以我们可以在该目录下创建一个nginx文件装我们的nginx。
[root@qmfz qmfz]# mkdir nginx
[root@qmfz qmfz]# cd nginx 进入我们的目录下载nginx的压缩包
[root@qmfz nginx]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
--2021-01-20 11:27:31-- http://nginx.org/download/nginx-1.14.2.tar.gz
正在解析主机 nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5704::6, ...
正在连接 nginx.org (nginx.org)|52.58.199.22|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1015384 (992K) [application/octet-stream]
正在保存至: “nginx-1.14.2.tar.gz”
100%[===================================================>] 1,015,384 23.1KB/s 用时 32s
2021-01-20 11:28:04 (31.2 KB/s) - 已保存 “nginx-1.14.2.tar.gz” [1015384/1015384])
[root@qmfz nginx]# tar -zxvf nginx-1.14.2.tar.gz(然后解压我们的安装包)
nginx-1.14.2/
nginx-1.14.2/auto/
nginx-1.14.2/conf/
nginx-1.14.2/contrib/
nginx-1.14.2/src/
nginx-1.14.2/configure
nginx-1.14.2/LICENSE
nginx-1.14.2/README
nginx-1.14.2/html/
nginx-1.14.2/man/
nginx-1.14.2/CHANGES.ru
nginx-1.14.2/CHANGES
nginx-1.14.2/man/nginx.8
nginx-1.14.2/html/50x.html
- 开始安装
[root@qmfz nginx]# cd nginx-1.14.2(进入我们的nginx的目录)
[root@qmfz nginx-1.14.2]# ll
总用量 732
drwxr-xr-x. 6 1001 1001 4096 1月 20 11:29 auto
-rw-r--r--. 1 1001 1001 288742 12月 4 2018 CHANGES
-rw-r--r--. 1 1001 1001 440121 12月 4 2018 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 1月 20 11:29 conf
-rwxr-xr-x. 1 1001 1001 2502 12月 4 2018 configure
drwxr-xr-x. 4 1001 1001 72 1月 20 11:29 contrib
drwxr-xr-x. 2 1001 1001 40 1月 20 11:29 html
-rw-r--r--. 1 1001 1001 1397 12月 4 2018 LICENSE
drwxr-xr-x. 2 1001 1001 21 1月 20 11:29 man
-rw-r--r--. 1 1001 1001 49 12月 4 2018 README
drwxr-xr-x. 9 1001 1001 91 1月 20 11:29 src
[root@qmfz nginx-1.14.2]# ./configure && make && make install(无参数类,如只需要完成我们的安装,这条命令就可以满足)
[root@qmfz nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_v2_module && make && make install
参数:
--prefix nginx的安装根路径
--sbin nginx的启动程序路径
--conf-path nginx的配置路径
--with- nginx需要加载的模块,如果需要nginx支持https,则需要加入http_ssl_module模块
以上配置参数可根据自身实际情况相应调整。
- 配置环境变量,这样的话就可以在任何目录使用nginx命令
[root@qmfz nginx]# vim /etc/profile
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
打开该文件,在文末加上:
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
[root@qmfz nginx-1.14.2]# source /etc/profile 让配置生效
- 设置nginx为系统服务
[root@qmfz nginx]# vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx service
After=network target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx (安装路径)
ExecReload=/usr/local/nginx/sbin/nginx-s reload (安装路径)
ExecStop=/usr/local/nginx/sbin/nginx-s quit (安装路径)
PrivateTmp=true
[Install]
WantedBy=multi-user.target
~
- 设置开机自启动
[root@qmfz nginx]# systemctl enable nginx
- 如果你不想设置这些就直接执行文件
[root@qmfz nginx-1.14.2]# cd /usr/local/nginx/sbin 进入我们的安装目录
[root@qmfz sbin]# ./nginx -s reload 重启
- 安装好后用ip地址就可以直接访问到nginx的页面
配置Nginx的反向代理
- 在安装nginx目录下有个装配置文件的目录conf,新建一个vhost目录,再新建一个文件(以conf后缀结尾)
[root@qmfz conf]# mkdir vhost
[root@qmfz conf]# ll
总用量 68
-rw-r--r--. 1 root root 1077 1月 20 11:32 fastcgi.conf
-rw-r--r--. 1 root root 1077 1月 20 11:54 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 1月 20 11:32 fastcgi_params
-rw-r--r--. 1 root root 1007 1月 20 11:54 fastcgi_params.default
-rw-r--r--. 1 root root 2837 1月 20 11:54 koi-utf
-rw-r--r--. 1 root root 2223 1月 20 11:54 koi-win
-rw-r--r--. 1 root root 5170 1月 20 11:32 mime.types
-rw-r--r--. 1 root root 5170 1月 20 11:54 mime.types.default
-rw-r--r--. 1 root root 2656 1月 20 11:32 nginx.conf
-rw-r--r--. 1 root root 2656 1月 20 11:54 nginx.conf.default
-rw-r--r--. 1 root root 636 1月 20 11:32 scgi_params
-rw-r--r--. 1 root root 636 1月 20 11:54 scgi_params.default
-rw-r--r--. 1 root root 664 1月 20 11:32 uwsgi_params
-rw-r--r--. 1 root root 664 1月 20 11:54 uwsgi_params.default
drwxr-xr-x. 2 root root 6 1月 20 13:25 vhost
-rw-r--r--. 1 root root 3610 1月 20 11:54 win-utf
[root@qmfz conf]# cd vhost
[root@qmfz vhost]# vim chenshuyi.biuayi.cn.conf
- 在该文件里添加一下内容
server {
listen 80;
autoindex on;
server_name chenshuyi.biuayi.cn; (我们域名)
access_log /usr/local/nginx/logs/access.log combined;
index index.html index.htm index.jsp index.php;
if ( $query_string ~* ".*[\;'\<\>].*" ){
return 404;
}
location / {
proxy_pass http://127.0.0.1:8080;
add_header Access-Control-Allow-Origin *;
}
}
- 写完后进入我们的nginx配置文件,在http节点下添加以下内容
include vhost/*.conf;
- 重启我们的nginx,保证Tomcat是启动状态。
[root@qmfz conf]# cd /usr/local/nginx/sbin
[root@qmfz sbin]# ./nginx -s reload
最后使用我们的域名直接访问(已成功)