Nginx反向代理使用【转载】
最近工作中经常使用nginx,为了能够更好的使用nginx,我搜罗了很多nginx相关的技术文章来读,所以才有了下面以下内容。在此,为文中引用到和参考到的文章提供者表示感谢。如文中相关内容有错误,也欢迎大家留言提出。
Nginx的安装与启动
1)pcre安装,支持正则表达式
http://www.pcre.org/
# tar zxvf pcre-7.9.tar.gz
# cd pcre-7.9
#./configure
# make && make install
2)openssl安装(可选),支持安全协议的站点
http://www.openssl.org/
# tar zxvf openssl-0.9.8l.tar.gz
# cd openssl-0.9.8l
#./config
# make && make install
3)nginx的安装
# tar zxvf nginx-0.7.64.tar.gz
# cd nginx-0.7.64
4)启动
# cd usr/local/nginx/sbin
# /nginx
5)重启
./nginx -s reload
Nginx.conf 基本配置
#user nobody;
worker_processes 1; #nginx进程数,建议设置为等于CPU总核心数。
events {
worker_connections 1024; #单个进程最大连接数(最大连接数=连接数*进程数)
}
http {
include mime.types;#文件扩展名与文件类型映射表
default_type application/octet-stream;#默认文件类型
sendfile on;#sendfile on; #开启高效文件传输模式
keepalive_timeout 60;#长连接超时时间,单位是秒
#gzip模块设置(略)
upstream blog.ha97.com {
#upstream的负载均衡,weight是权重,可以根据机器配置定义权重
#weigth参数表示权值,权值越高被分配到的几率越大。
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://192.168.24.152:8080/test1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /test {
proxy_pass http://192.168.24.152:8080/abc;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server{
listen 8082;#监听端口
server_name localhost;
location / {
proxy_pass http://192.168.24.152:8080/test1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
Nginx.conf server配置说明
1) server配置说明
server {
listen 80; #监听端口
server_name localhost; #域名可以有多个,用空格隔开
location / {
proxy_pass http://192.168.24.152:8080/test1;
proxy_set_header X-Real-IP $remote_addr;
#把真实的IP发送给 转发的web服务器,否则转发的IP是nginx这台的IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#意思是增加一个$proxy_add_x_forwarded_for到X-Forwarded-For里去
#例X-Forwarded-For: 1.1.1.1, 2.2.2.2, 3.3.3.3 代表 请求由1.1.1.1发出,
#经过三层代理,#第一层是2.2.2.2,第二层是3.3.3.3,而本次请求的来源
#IP4.4.4.4是第三层代理
}
}
2)多个server配置,监听不同端口
server { #server1
listen 80;
server_name localhost;
location / {
proxy_pass http://192.168.24.152:8080/test1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server{ #server2
listen 8082;#监听端口
server_name localhost;
location / {
proxy_pass http://192.168.24.152:8080/test1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server配置路径匹配问题
Nginx location中可能涉及的匹配规则有:
= 精确匹配
^~ 普通字符匹配,区分大小写
~ 正则匹配,区分大小写
/xxx/yyy.zzz 最长匹配
/
其优先级顺序如下:
=优先级大于^~
^~优先级大于~
~ 对比 /xxx/yyy.zzz
^~优先级大于/xxx/yyy.zzz
~优先级大于/
/xxx/yyy.zzz优先级大于/
/xxx/yyy/ 优先级大于 /xxx/
综上所述location规则优先级顺序为 = > ^~ > ~ > /xxx/yyy.zzz > / ,路径匹配的优先级与location在文档中的位置先后无关。
Nginx实现ssl反向代理
server {
listen 443; #监听443端口
server_name localhost;
ssl on; #启用ssl加密
ssl_certificate /ect/cert/free4lab.crt; #服务器证书crt文件
ssl_certificate_key /ect/cert/free4lab.key; #服务器私钥key文件
location / {
proxy_pass http://192.168.24.152:8080/test1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
转自:
实战录 | 今天聊聊Nginx反向代理使用 - 今日头条(TouTiao.com)
http://toutiao.com/a6309953445340594433/?tt_from=mobile_qq&utm_campaign=client_share&app=news_article&utm_source=mobile_qq&iid=4913332425&utm_medium=toutiao_ios
Nginx反向代理使用【转载】的更多相关文章
- [转载]Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统 ...
- Nginx 反向代理 -- 一路上的坑转载
个人学习之用转子https://www.cnblogs.com/xjbBill/p/7477825.html 前些天刚过来新公司上班,公司的项目都挺多的,只不过项目都是第三方公司团队开发的,现在本公司 ...
- Nginx反向代理,负载均衡,redis session共享,keepalived高可用
相关知识自行搜索,直接上干货... 使用的资源: nginx主服务器一台,nginx备服务器一台,使用keepalived进行宕机切换. tomcat服务器两台,由nginx进行反向代理和负载均衡,此 ...
- Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
转载:http://freeloda.blog.51cto.com/2033581/1288553 大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负 ...
- NGINX反向代理
Nginx反向代理 ...
- 最简单实现跨域的方法:用 Nginx 反向代理
本文作者: 伯乐在线 - 良少 .未经作者许可,禁止转载!欢迎加入伯乐在线 专栏作者. 什么是跨域 跨域,指的是浏览器不能执行其他网站的脚本.它是由浏览器的同源策略造成的,是浏览器对javascrip ...
- Nginx反向代理以及负载均衡配置
项目地址:http://git.oschina.net/miki-long/nginx 前提:最近在研究nginx的用法,在windows上小试了一下,由于windows下不支持nginx缓存配置,所 ...
- Nginx反向代理上传大文件报错(failed to load resource : net :: ERR_CONNECTION_RESET)
转自: https://blog.csdn.net/kinginblue/article/details/50753271?locationNum=14&fps=1 Nginx反向代理上传大文 ...
- 谁说前端不需要懂-Nginx反向代理与负载均衡
转:https://juejin.im/post/5b01336af265da0b8a67e5c9 学到老活到老 前端圈一直很新,一直要不停的学习,而且在进入大厂的路上,还要求熟悉一门后台语言等等.用 ...
随机推荐
- Memcached启动脚本
ched: MemCached Daemon # # chkconfig: - # description: MemCached Daemon # # Source function library. ...
- 安卓Activity组件
Activity生命周期 熟悉javaEE的朋友们都了解servlet技术,我们想要实现一个自己的servlet,需要继承相应的基类,重写它的方法,这些方法会在合适的时间被servlet容器调用.其 ...
- 说说final关键字(好像有干货)
在java开发过程中,final是大家常用的关键字,无非就是用来修饰类,方法和变量,来表名类不能被继承,方法不会被覆盖,变量不能被改变,悄悄的说一句,private方法也隐式的final.通过一段时间 ...
- Windows Access Token
security descriptor A structure and associated data that contains the security information for a sec ...
- JavaScript高级程序设计:第七章
函数表达式 1.函数表达式的特征: 定义函数的方式有两种:一种是函数声明,另一种就是函数表达式.函数声明的语法是这样的: function functionName(arg0,arg1,arg2){ ...
- Bootstrap 3 与 Foundation 5
开发工程师, 使用 Bootstrap. 前端开发人员, 使用 Foundation. 我们来谈谈为什么. Bootstrap 与 Foundation 有许多关键的区别, 但是, 我想你只需要记住一 ...
- Ansible5:常用模块【转】
根据zs官方的分类,将模块按功能分类为:云模块.命令模块.数据库模块.文件模块.资产模块.消息模块.监控模块.网络模块.通知模块.包管理模块.源码控制模块.系统模块.单元模块.web设施模块.wind ...
- 在vim中使用perltidy美化perl代码
来源: http://www.cnblogs.com/itech/archive/2013/02/18/2915279.html 格式优美的perl代码不但让人赏心悦目,而且可以方便阅读. perlt ...
- three dot
http://stackoverflow.com/questions/28031603/what-do-three-dots-mean-in-go-command-line-invocations
- wpf之ListBox横向显示所有ListBoxItem
Xaml: <Window x:Class="WpfApplication6.MainWindow" xmlns="http://schemas.microsoft ...