与众不同的生活方式很累人呢,因为找不到借口

    在上一章节中,我们以及了解到正向、反向代理、负载均衡和动静分离的基本概念,安装教程,而在本节中将会了解到在

本文要点:

1.理清概念

2.Linux下Nginx的常用命令

3.Nginx的基本配置文件nginx.conf

4.Nginx 配置实例-反向代理实例 1

1.理清概念

docker创建Nginx容器很简单:共享数据卷,外部访问端口映射

window下运行nginx,只需简单式一直点击“NEXT”安装,然后添加到环境变量即可

在Linux下启动nginx相对比较麻烦,如果你采用其它两种方式,可以跳过本节,本节主要是介绍Linux下的运行命令

无论哪种方式启动Nginx,其核心的配置文件以及接下来的几个配置实例都是通用的

2.Linux下Nginx的常用命令

先关闭防火墙或者是开放目标端口

进入nginx目录中

cd /usr/local/nginx/sbin (前面路径有可能不同,主要是nginx内的nginx/sbin路径)

(1)、查看nginx版本号

./nginx -v

(2)、启动nginx

./nginx

(3)、停止nginx

./nginx -s stop

(4)、重新加载nginx

./nginx -s reload

3.ginx的基本配置文件nginx.conf

3.1、nginx 配置文件位置

cd /usr/local/nginx/conf/nginx.conf

3.2、配置文件中的内容 包含三部分内容

(1)全局块:配置服务器整体运行的配置指令 比如 worker_processes 1;处理并发数的配置

(2)events 块:影响 Nginx 服务器与用户的网络连接 比如 worker_connections 1024; 支持的最大连接数为 1024

(3)http 块 还包含两部分: http 全局块 server 块

3.3 默认的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;
} 404.md # 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;
# }
#}
}

4.Nginx 配置实例-反向代理实例 1

4.1、实现效果

(1)打开浏览器,在浏览器地址栏输入地址 www.123.com,跳转到 liunx 系统 tomcat 主页 面中

4.2、准备工作

(1)在 liunx 系统安装 tomcat,使用默认端口 8080 * tomcat 安装文件放到 liunx 系统中,解压 * 进入 tomcat 的 bin 目录中,./startup.sh 启动 tomcat 服务器

(2)对外开放访问的端口

firewall-cmd --add-port=8080/tcp --permanent

firewall-cmd --reload

(3)查看已经开放的端口号

firewall-cmd --list-all

(4)在 windows 系统中通过浏览器访问 tomcat 服务器(linux_IP + tomcat_Port)

4.3、访问过程分析

    (1)映射IP

假设域名www.123.com映射的IP为Linux上的nginx的ip地址(比如我的是192.168.17.129:80),nginx也相当于一个分发器,将请求发送至tomcat服务器

访问过程分析
windows端 ---------->Nginx(linux_IP:80)------>tomcat(127.0.0.1:8080)
(假设访问www.123.com)

    (2)在 nginx 进行请求转发的配置(反向代理配置)

server{
listen 80; # 监听端口
server_name www.123.com; # 配置域名 location /{
proxy_pass http://127.0.0.1:8080; # 跳转到127.0.0.1:8080路径
index index.html index.htm index.jsp;
}
}

如上配置,我们监听 80 端口,访问域名为 www.123.com,不加端口号时默认为 80 端口,故 访问该域名时会跳转到 127.0.0.1:8080 路径上。在浏览器端输入 www.123.com后,成功后直接跳转到tomcat主页

Nginx:反向代理的更多相关文章

  1. 使用python自动生成docker nginx反向代理配置

    由于在测试环境上用docker部署了多个应用,而且他们的端口有的相同,有的又不相同,数量也比较多,在使用jenkins发版本的时候,不好配置,于是想要写一个脚本,能在docker 容器创建.停止的时候 ...

  2. Nginx反向代理,负载均衡,redis session共享,keepalived高可用

    相关知识自行搜索,直接上干货... 使用的资源: nginx主服务器一台,nginx备服务器一台,使用keepalived进行宕机切换. tomcat服务器两台,由nginx进行反向代理和负载均衡,此 ...

  3. Nginx反向代理部署指南

    一.反向代理 我们都知道,80端口是web服务的默认端口,其他主机访问web服务器也是默认和80端口进行web交互,而一台服务器也只有一个80端口,这是约定俗成的标准. 我们来看下面两个场景: 1.服 ...

  4. Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解

    转载:http://freeloda.blog.51cto.com/2033581/1288553 大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负 ...

  5. Nginx反向代理和负载均衡

    一.Nginx反向代理设置 从80端口转向其他端口反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的 ...

  6. nginx 反向代理

    nginx 反向代理 vim nginx.conf http { ..... upstream "tomcatweb" { server 172.30.13.199:8080; s ...

  7. 关于nginx反向代理后获取不到客户端的真实ip地址问题

    前段时间在我的网站上用nginx做了一下反向代理,最近发现不能获取客户端ip了,都是拿到的127.0.0.1的本地ip... 通过查资料后,再去看了看我的配置文件,结果发现我没有如下配置: nginx ...

  8. Nginx反向代理配置可跨域

    由于业务需要,同一项目中的前端代码放在静态环境中,而后端代码放在tomcat中,但此时问题却出现了:前端使用ajax请求后端获取数据时出现如下报错 XMLHttpRequest cannot load ...

  9. Nginx反向代理搭建配置

    1.反向代理方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将服务器上得到的结果返回给internet 上请求连接的客户端,此时代理服务器对外就表现为一个 ...

  10. nginx反向代理docker registry报”blob upload unknown"解决办法

    问题症状:keepalived+nginx反向代理后端docker registry群集时,使用docker客户机向registry push镜像时出现 "blob upload unkno ...

随机推荐

  1. 原生js 复制内容到剪切板

    代码: function copyHandle(content){ let copy = (e)=>{ e.preventDefault() e.clipboardData.setData('t ...

  2. 一款简单的C++猜数字游戏(新手必学)

    前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:1只小弛 废话不多说,直接上代码! #include<bits/s ...

  3. 【React】345- React v16.9 新特性[译]

    今天我们发布了 React 16.9.它包含了一些新特性.bug修复以及新的弃用警告,以便与筹备接下来的主要版本. 一.新弃用 重命名 Unsafe 生命周期方法 一年前,我们宣布 unsafe 生命 ...

  4. Electron:主进程和渲染进程

  5. 【Feign】@FeignClient相同名字错误 The bean 'xxx.FeignClientSpecification', defined in null, could not be registered

    The bean 'xxx.FeignClientSpecification', defined in null, could not be registered. A bean with that ...

  6. JAVA可视化闹钟源码

    概述 一些同学的Java课设有这样一个问题,比较感兴趣就做了一下 功能介绍: 1.可增加闹钟 2.可删除闹钟 3.时间到了响铃 4.关闭闹钟不会丢失闹钟(因为闹钟存储在txt文件中,不会因程序关闭就终 ...

  7. CentOS7安装部署squid服务(透明代理+反向代理)

    一.squid服务介绍 Squid是一个高性能的代理缓存服务器,Squid支持FTP.gopher.HTTPS和HTTP协议.和一般的代理缓存软件不同,Squid用一个单独的.非模块化的.I/O驱动的 ...

  8. linux目录的读(r)、写(w)、执行(x)权限说明

    linux目录的读.写.执行权限说明 1.可读r #表示具有浏览目录下面文件及子目录的权限.即ls dir 1)如果没有x权限,不能进到目录里,即无法 cd dir 2)如果没有x权限,ls列表可以看 ...

  9. Spring Cloud第九篇 | 分布式服务跟踪Sleuth

    ​ ​本文是Spring Cloud专栏的第九篇文章,了解前八篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...

  10. C# 使用自带Microsoft.Office.Interop.Excel简单操作Excel文件

    项目添加应用 Microsoft.Office.Interop.Excel.dll 文件 引用命名空间: using Excel = Microsoft.Office.Interop.Excel; 简 ...