【Nginx】Nginx在Linux下的入门介绍
Nginx的安装
下载、解压
从Nginx下载安装包,我下的是nginx-1.8.0.tar.gz。解压后的目录为:
[root@blog third_package]# tar -zxf nginx-1.8.0.tar.gz
[root@blog third_package]# ll nginx-1.8.0
total 652
drwxr-xr-x 6 1001 1001 4096 Jul 23 18:17 auto
-rw-r--r-- 1 1001 1001 249124 Apr 21 2015 CHANGES
-rw-r--r-- 1 1001 1001 379021 Apr 21 2015 CHANGES.ru
drwxr-xr-x 2 1001 1001 4096 Jul 23 18:17 conf
-rwxr-xr-x 1 1001 1001 2478 Apr 21 2015 configure
drwxr-xr-x 4 1001 1001 4096 Jul 23 18:17 contrib
drwxr-xr-x 2 1001 1001 4096 Jul 23 18:17 html
-rw-r--r-- 1 1001 1001 1397 Apr 21 2015 LICENSE
drwxr-xr-x 2 1001 1001 4096 Jul 23 18:17 man
-rw-r--r-- 1 1001 1001 49 Apr 21 2015 README
drwxr-xr-x 8 1001 1001 4096 Jul 23 18:17 src
依赖的软件
安装之前把依赖的软件装上,我这里用YUM:yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
安装
[root@blog nginx-1.8.0]# pwd
/installation_package/nginx-1.8.0
[root@blog nginx-1.8.0]#
[root@blog nginx-1.8.0]# ./configure --prefix=/opt/nginx_1
执行./configure后在添加了一个目录objs,--prefix表示安装到此目录,如果不设置默认安装到/usr/local/nginx。
编译工作:
make
make install
启动
用/opt/nginx_1/sbin/nginx启动,默认使用的是安装目录的NGINX_HOME/conf/nginx.conf,也就是/opt/nginx_1/conf/nginx.conf。
当然,也可以使用/opt/nginx_1/sbin/nginx -c /opt/nginx_1/conf/nginx.conf指定配置文件。
Nginx的反向代理
我们常用Nginx做反向代理,在设置反向代理前,应先了解下正向代理和反向代理。
如何设置
将到达Nginx的请求转到后端具体的主机,可通过设置上游服务器和代理转发。比如:
http {
...
upstream myweb {
server 127.0.0.1:9999;
}
server {
...
location /myweb {
proxy_pass http://myweb;
}
}
}
设置好之后,将上游服务器127.0.0.1:9999也部署好,就可以通过Nginx享受上游服务器的具体服务了。
但要注意请求的信息的转发,比如后端是一台TOMCAT,里面运行一个Servet打印各项参数:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StringBuffer sb = new StringBuffer();
sb.append("request.getProtocol() : " + request.getProtocol()).append("\n");
sb.append("request.getScheme() : " + request.getScheme()).append("\n");
sb.append("request.getRemoteAddr() : " + request.getRemoteAddr()).append("\n");
sb.append("request.getRemoteHost() : " + request.getRemoteHost()).append("\n");
sb.append("request.getServerPort() : " + request.getServerPort()).append("\n");
sb.append("request.getRemotePort() : " + request.getRemotePort()).append("\n");
sb.append("request.getQueryString() : " + request.getQueryString()).append("\n");
sb.append("request.getRemoteUser() : " + request.getRemoteUser()).append("\n");
sb.append("request.getMethod() : " + request.getMethod()).append("\n");
sb.append("request.getLocalAddr() : " + request.getLocalAddr()).append("\n");
sb.append("request.getLocalName() : " + request.getLocalName()).append("\n");
sb.append("request.getPathInfo() : " + request.getPathInfo()).append("\n");
sb.append("request.getRequestURI() : " + request.getRequestURI()).append("\n");
sb.append("request.getRequestURL() : " + request.getRequestURL()).append("\n");
sb.append("request.getContextPath() : " + request.getContextPath()).append("\n");
response.getWriter().append("Served at: ").append(request.getContextPath()).append("\n").append(sb);
}
直接访问TOMCAT,http://nick-huang.com:9999/myweb/PrintEnvInfoServlet?keyword=hello-world,打印的信息是这样的:
Served at: /myweb
request.getProtocol() : HTTP/1.1
request.getScheme() : http
request.getRemoteAddr() : 客户端IP
request.getRemoteHost() : 客户端IP
request.getServerPort() : 9999
request.getRemotePort() : 64494
request.getQueryString() : keyword=hello-world
request.getRemoteUser() : null
request.getMethod() : GET
request.getLocalAddr() : 服务端IP
request.getLocalName() : 服务端IP
request.getPathInfo() : null
request.getRequestURI() : /myweb/PrintEnvInfoServlet
request.getRequestURL() : http://nick-huang.com:9999/myweb/PrintEnvInfoServlet
request.getContextPath() : /myweb
只作反向代理的设置,访问NGINX,https://nick-huang.com:777/myweb/PrintEnvInfoServlet?keyword=hello-world,后打印:
Served at: /myweb
request.getProtocol() : HTTP/1.0
request.getScheme() : http
request.getRemoteAddr() : 127.0.0.1
request.getRemoteHost() : 127.0.0.1
request.getServerPort() : 80
request.getRemotePort() : 54856
request.getQueryString() : keyword=hello-world
request.getRemoteUser() : null
request.getMethod() : GET
request.getLocalAddr() : 127.0.0.1
request.getLocalName() : localhost
request.getPathInfo() : null
request.getRequestURI() : /myweb/PrintEnvInfoServlet
request.getRequestURL() : http://myweb/myweb/PrintEnvInfoServlet
request.getContextPath() : /myweb
反向代理后的请求头信息传递
可以发现,反向代理后Protocol、RemoteAddr、ServerPort、RequestURL等参数均有所不同,那么我们需要设置代理时传递参数。
Nginx配置:
upstream myweb {
server 127.0.0.1:9999;
keepalive 32;
}
...
location /myweb {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://myweb;
}
相关说明,请点击链接:proxy_http_version、keepalive。
Tomcat的/conf/server.xml的Host节点下添加:
<Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For" protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https" httpsServerPort="777" />
相关说明,请点击链接:org.apache.catalina.valves Class RemoteIpValve
访问https://nick-huang.com:777/myweb/PrintEnvInfoServlet?keyword=hello-world,日志是这样的:
Served at: /myweb
request.getProtocol() : HTTP/1.1
request.getScheme() : https
request.getRemoteAddr() : 客户端IP
request.getRemoteHost() : 客户端IP
request.getServerPort() : 777
request.getRemotePort() : 55022
request.getQueryString() : keyword=hello-world
request.getRemoteUser() : null
request.getMethod() : GET
request.getLocalAddr() : 127.0.0.1
request.getLocalName() : localhost
request.getPathInfo() : null
request.getRequestURI() : /myweb/PrintEnvInfoServlet
request.getRequestURL() : https://nick-huang.com:777/myweb/PrintEnvInfoServlet
request.getContextPath() : /myweb
【Nginx】Nginx在Linux下的入门介绍的更多相关文章
- [转]Linux下的图形库介绍
[转]Linux 下的图形库介绍 http://blog.csdn.net/gogor/article/details/5925925 在进行Linux下的图形系统编程时,我们常常会遇到以下这些概念: ...
- Linux 下安装 resync 介绍
Linux 下安装 resync 介绍 这是官网,找到对应版本的下载地址. 这里提供Linux_X64的安装包 wget '' https://download-cdn.resilio.com/sta ...
- nginx 系列 1 linux下安装以及配置IIS分发
一. 安装 操作系统:centos 7 ,nginx版本1.12.2,windows server 2008 iis 1.1 确认nginx所依赖的工具 Zlib: nginx提供gzip模块,需要 ...
- Nginx(一):linux下安装nginx与配置
linux系统为Centos 64位 准备目录 [root@instance-3lm099to ~]# mkdir /usr/local/nginx [root@instance-3lm099to ~ ...
- Nginx --Windows下和Linux下搭建集群小记
nginx: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器 特点: 反向代理 负载均衡 动静分离... 反向代理 : 先来了解正向代理:需要我们用户 ...
- uWSGI+Nginx+Flask在Linux下的部署
搞了一天多,终于搞通了uWSGI的部署原理,下面总结一下遇到的一些坑,希望给读者能够少走弯路. 简单来说,uWSGI是一个web服务器,Nginx进行反向代理的其实跟这些服务器可以说没有 ...
- 第一阶段·Linux运维基础-第1章·Linux基础及入门介绍
01-课程介绍-学习流程 02-服务器硬件-详解 03-服务器核心硬件-服务器型号-电源-CPU 01-课程介绍-学习流程 1.1. 光看不练,等于白干: 1.2 不看光练,思想怠慢: 1.3 即看又 ...
- Linux 下的图形库介绍
在进行Linux下的图形系统编程时,我们常常会遇到以下这些概念: Framebuffer, X11, SDL,DFB, miniGUI, OpenGL,QT, GTK,KDE, GNOME等等. 一. ...
- Linux下Wheel用户组介绍
昨天遇到一个很奇怪的事情,有一台服务器在使用su - root命令切换到root账号时,老是报密码不正确.但是root密码完全是正确的,而且可以使用账号密码直接ssh登录服务器.很是纳闷,如下所示: ...
随机推荐
- 安装win7出现安装程序无法创建新的系统分区
安装win7的时候出现“安装程序无法创建新的系统分区 也无法定位系统分区”! 我是直接把一个系统碟里面的安装文件全部拷出来.放到要安装系统的机器(D盘).用的是老毛桃的winpe已经安装好了.我的安装 ...
- SpringMVC(十七-二十) ModelAttribute 注解
有点难理解. 修饰方法是表示在该控制器的所有目标方法执行前都执行该modelattribute注解的方法. 修饰参数是表示什么?修饰参数时@modelattributes(value="xx ...
- ORA-01440 要减小精度或者标度.则要修改的列必须为空.
--1 创建备份表 drop table contract_kangjia_back; create table contract_kangjia_back as select * from cont ...
- SpringBoot多数据源
很多业务场景都需要使用到多数据库,本文介绍springboot对多数据源的使用. 这次先说一下application.properties文件,分别连接了2个数据库test和test1.完整代码如下: ...
- 快速上手Git
本文主要摘录于廖雪峰的Git教程,个别地方做了可能不恰当的修改或补充,主要方便自己回顾.查看更详细内容请移步廖老师博客.同时,感谢廖老师写出这么好的入门指导. (有彩蛋!!!) 一.热身 1.初始化一 ...
- [mariadb]Windows Mariadb 10.2安装过程
在学习Flask的过程中,碰到SQLAlchemy不支持Mariadb 10.2.9以前版本的问题,于是升级Mariadb到10.2.10. 升级过程中,我只能说,Mariadb及Mysql的文档结构 ...
- 谈一谈java里面的反射机制
首先来看看百度百科中是如何定义的: JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方 ...
- BZOJ2167 : 公交车站
设$f[i]$表示$i$往上通过一趟公交车能到达的深度最小的祖先,这可以通过将公交车按$lca$深度从小到大排序后用并查集染色得到. 对于每个询问: $1.x==y$ $ans=0$. $2.x$是$ ...
- select超全超详细总结篇
1.经常使用到select,每次都是翻翻翻资料,干脆总结一下,方便日后查看. 2.checked 匹配所有选中的被选中元素(复选框.单选框等,不包括select中的option) se ...
- websocket是什么
websocket是什么? 这是知乎上面一个作者写的一篇风趣幽默又容易理解关于 websocket 的文章. 提供一下连接地址:https://www.zhihu.com/question/20215 ...