nginx代理 1个端口+路径匹配 代理多个web
目录
- nginx安装及卸载
- 修改配置文件
- 注意事项
- 记忆例子
nginx用yum安装在主机里,之前发过文章是装进容器里的,目录上稍有区别,配置文件基本一致,在主机内用的root用户,我部署nginx的主机地址是192.168.100.11,端口用的28080
nginx安装及卸载
1、nginx安装
用以下指令安装之后,就可以直接用了,如果有firewalld,可以选择关闭,也可以选择做端口放行
[root@localhost ~]# yum -y install epel-release #安装epel源
[root@localhost ~]# yum -y install nginx #安装最新版nginx
[root@localhost ~]# nginx -v #查看版本
[root@localhost ~]# systemctl start nginx #启动nginx
2、nginx使用过程中可能用到的指令
systemctl start nginx #启动nginx
systemctl stop nginx #关闭nginx
systemctl restart nginx #重启nginx
systemctl enable nginx #设置开机自启动
systemctl disable nginx #关闭开机自启动
nginx -t #检查配置文件
nginx -s reload #重载
systemctl start firewalld #开启闭防火墙
systemctl stop firewalld #关闭防火墙
systemctl enable firewalld #永久开启闭防火墙
systemctl disable firewalld #永久关闭防火墙
firewall-cmd --zone=public --permanent --add-port=80/tcp #防火墙永久放行80端口
firewall-cmd --zone=public --permanent --remove-port=80/tcp #防火墙永久关闭放行80端口
firewall-cmd --reload #重载
3、nginx卸载
[root@localhost ~]# systemctl stop nginx #关闭nginx
[root@localhost ~]# systemctl disable nginx #关闭开机自启动
[root@localhost ~]# find / -name nginx #查找nginx相关路径,然后用 rm -rf 删除
[root@localhost ~]# rm -rf /etc/nginx #举例删除,用不好的话,不能随便使用这个指令
[root@localhost ~]# yum -y remove nginx #卸载nginx
修改配置文件
因为我们的程序里有websocket,所以配置文件里增加了一点东西,在文件中标注了“ #看这里”字样的行仔细看看就行
[root@localhost ~]# vim /etc/nginx/nginx.conf #编辑配置文件# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;events {worker_connections 1024;
}http {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 /var/log/nginx/access.log main;sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 4096;include /etc/nginx/mime.types;default_type application/octet-stream;# Load modular configuration files from the /etc/nginx/conf.d directory.# See http://nginx.org/en/docs/ngx_core_module.html#include# for more information.include /etc/nginx/conf.d/*.conf;map $http_upgrade $connection_upgrade { # htpp升级为ws用default upgrade;#'' close;}server {listen 28080; #看这里listen [::]:28080; #看这里server_name _;root /usr/share/nginx/html;location / { #看这里proxy_pass http://192.168.100.11:8080; #看这里} #看这里location /aj { # htpp升级为ws用proxy_pass http://192.168.100.11:8888;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";}location /dyz { #看这里proxy_pass http://192.168.100.19:8001/; #看这里} #看这里location /multipower/ {proxy_pass http://192.168.10.23:80; #看这里}# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;error_page 404 /404.html;location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}# Settings for a TLS enabled server.
#
# server {# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {# }
# }}
注意事项
nginx主机地址192.168.100.11,端口28080
配置文件中代理配置的两个部分有区别,我建议通过路径转发的话,路径后边加上“/”,更容易理解;
location /dyz {proxy_pass http://192.168.100.19:8001/;
}
# 第1个location部分,我写的“/dyz” ,“proxy_pass http://192.168.100.19:8001/;” ,在8001后边不带“/”,这个代理就有问题,就实现不了,这里这个问题研究了一个多小时才解决
#效果如下:
http://192.168.100.19:8001/login.html 等效于 http://192.168.100.11:28080/dyz/login.html
location /multipower/ {proxy_pass http://192.168.10.23:80;
}
# 第2个location部分,我写的“/multipower/” ,“proxy_pass http://192.168.10.23:80;” ,# 如果在80后边不带“/” ,那么“/multipower/”就会补全到url的80端口末尾处,效果如下:
http://192.168.100.23:80/multipower/login.html 等效于 http://192.168.100.11:28080/multipower/login.html# 如果在80后边带“/” ,那么“/multipower/”就不会补全到url的80端口末尾处,效果如下:
http://192.168.100.23:80/multipower/login.html 等效于 http://192.168.100.11:28080/login.html
记忆例子
以下是帮助我个人的记忆方法,注意带与不带 “/”
写法 1:(路径追加至末尾)
location /multipower/ {
proxy_pass http://192.168.10.23:80;
}
http://192.168.100.11:28080/multipower/login.html
代理到 http://192.168.10.23:80/multipower/login.html
写法 2:(路径后的内容替换,“/multipower/*” 与“/192.168.10.23:80/*”)
location /multipower/ {
proxy_pass http://192.168.10.23:80/;
}
http://192.168.100.11:28080/multipower/login.html
代理到 http://192.168.10.23:80/login.html
写法 3:(路径内容替换,“/test/” 与 “/multipower/”)
location /test/ { # 其中 test 也可以写成 multipower ,这样效果和写法1效果一样
proxy_pass http://192.168.10.23:80/multipower/;
}
http://192.168.100.11:28080/test/login.html
代理到 http://192.168.10.23:80/multipower/login.html
写法 4:(路径内容替换成空的,“/multipower” 与 “192.168.10.23:80/*”)
location /multipower {
proxy_pass http://192.168.10.23:80/;
}
http://192.168.100.11:28080/multipower/login.html
代理到 http://192.168.10.23:80/login.html
写法5:(需要死记的例子)
location /multipower {
proxy_pass http://192.168.10.23:80/aaa;
}
http://192.168.100.11:28080/multipower/login.html
代理到 http://192.168.10.23:80/aaalogin.html
写完脑子有点乱,要是有错得地方,求帮忙指正
nginx代理 1个端口+路径匹配 代理多个web相关推荐
- 第九部分 代理的使用(代理设置,代理池的搭建,用代理获取微信公众号文章)...
网站采取的反爬虫措施有:弹出验证码,需要登录.检测某个IP在单位时间内的请求次数,超过规定的某个值,服务器拒绝服务,返回一些错误信息,这是封IP. 既然服务器封IP,可采用某种方式伪装IP,让服务器不 ...
- nginx反向代理(请求转发-URL匹配规则)
反向代理适用于很多场合,负载均衡是最普遍的用法. nginx 作为目前最流行的web服务器之一,可以很方便地实现反向代理. nginx 反向代理官方文档: NGINX REVERSE PROXY 当在 ...
- nginx请求转发被拒绝_nginx反向代理(请求转发-URL匹配规则)
反向代理适用于很多场合,负载均衡是最普遍的用法. nginx 作为目前最流行的web服务器之一,可以很方便地实现反向代理. 当在一台主机上部署了多个不同的web服务器,并且需要能在80端口同时访问这些 ...
- 使用nginx在指定端口做反向代理
使用nginx做反向代理 什么是代理.正向代理.反向代理? 代理 正向代理 反向代理 nginx最基本的组成部分 下载 配置文件 基础页面 nginx.exe 基本指令 nginx反向代理 官方文档的 ...
- nginx 反向代理时丢失端口的解决方案(转)
nginx 反向代理时丢失端口的解决方案(转) 参考文章: (1)nginx 反向代理时丢失端口的解决方案(转) (2)https://www.cnblogs.com/shenyixin/p/1136 ...
- nginx反向代理非80端口/nginx反代非80端口
首先声明,非专业研究nginx的,可以直接复制我的代码到 nginx 配置中再根据个人需求更改 非80端口通过80端口反向代理到非80端口应用,添加下面代码到nginx配置文件最后 server_na ...
- 利用Nginx反向代理解决80端口复用(内网域名转发)问题
公司多台服务器,但只有一个公网IP,80端口只好给公司网站使用 那么访问其他业务系统只能用反向代理做域名转发去实现 即:利用web服务器的80端口代理其它服务器的非80端口,实现80端口复用 反向代理 ...
- Nginx服务器中配置端口转发(反向代理)
部署Prometheus由于云主机只开放了80端口就用反向代理来做转发. 使用Nginx的proxy_pass配置项.Nginx监听80端口,接收到请求之后就会转发到要转发的URL. 配置如下 ser ...
- Nginx基础学习笔记(正向/反向代理,Nginx均衡负载和配置)
目录 1. 正向代理和反向代理 1.1 正向代理 (1)什么是正向代理? (2)正向代理的特点 (3)正向代理的用途 1.2 反向代理 (1)什么是反向代理? (2)反向代理的特点 (3)反向代理的用 ...
最新文章
- 整合NHibernate到Spring.Net (之一)
- 复习(一)—— Shell基本编程
- Android 屏幕灭屏亮屏广播,屏幕灭屏亮屏监听,广播实现按键监听
- “CSDN开发助手”:【必备插件 · 安装与使用教程】
- (补)20210623:力扣第246周周赛(下)
- Python豆瓣书籍信息爬虫
- ubuntu 的使用(三)—— 实用小工具
- SCWS中文分词,向xdb词库添加新词
- SharePoint开发错误—列表自定义表单出现“未将对象引用设置到对象的实例”
- python-正则表达式regex
- 软件开发 项目进展 软件架构 指南
- java流程图怎么画_计算机流程图怎么画
- (译)如何使用spritehelper和levelhelper教程:引子
- 51单片机IIC驱动OLED
- 流利阅读 2019 1.6 What happened at Theranos is a dazzling story of deception
- i78700k配什么显卡好_2K分辨率极致吃鸡 i7-8700K配GTX1070Ti吃鸡配置推荐 (全文)
- Element is missing end tag
- 公司文案编辑常用迅捷PDF转换成Word转换器
- Packetfence 开源网络准入系统
- 开源的app后台开源框架汇总
热门文章
- 初次买基金该怎样做?
- 《2023年电力安全监管重点任务》中的网络安全工作要求
- Python numpy random.randint函数
- 最好的风水,在君嘴上
- 任正非:80后90后是一代将星在闪烁
- PCA主成分分析实战案例
- 用户端无法连接 WebSocket connection to ‘ws://xxxx.xx.com:2345/‘ failed:Error in connection establish
- python dist的使用
- linux 进程及调度基础知识
- 在CentOS上安装Docker的步骤