【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登录服务器.很是纳闷,如下所示: ...
随机推荐
- SpringBoot使用WebFlux响应式编程操作数据库
这一篇文章介绍SpringBoot使用WebFlux响应式编程操作MongoDb数据库. 前言 在之前一篇简单介绍了WebFlux响应式编程的操作,我们在来看一下下图,可以看到,在目前的Spring ...
- BZOJ.3293.[CQOI2011]分金币(思路)
3293 双倍经验 1045 先考虑能否断环为链.显然是可以的,因为金币不可能在整个环上平移.所以我们枚举断点\(k\),表示\(k\)和\(k+1\)之间不交换金币. 令\(d_i=a_i-aver ...
- 2017-9-11-Linux开机启动脚本
参考文章:https://www.magentonotes.com/ubuntu-config-autostart-shell-script.html 还是先开门见山的说,Linux需要添加开机启动程 ...
- 3466 ACM Proud Merchants 变形的01背包
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3466 题意:假设你有M元,已经Pi,Qi,Vi(i为角标,1<i<N),当M>Qi,时才 ...
- Scrapy基础(十)———同步机制将Item中的数据写在Mysql
前面讲解到将Item中的所有字段都已经填写完成,那么接下来就是将他们存储到mysql数据库中,那就用到了pipeline项目管道了: 对项目管道的理解:做一个比喻,爬取好比是开采石油,Item装 ...
- 【DWM1000】 code 解密4一 ANCHOR 二进宫testapprun_s
上面我们的代码分析到ANCHOR 调用了一次testapprun_s,但是后面退出后发现还是满足while 条件,逼不得已还得再次调用testapprun_s.testapprun_s 也就是这样一点 ...
- [PA2014]Zadanie
[PA2014]Zadanie 题目大意: 一棵\(n(n\le3\times10^5)\)个点的树,每个点上有\(a_i\)个人.树上所有人到\(i\)号点距离之和为\(b_i\).已知\(\{b_ ...
- Linux下redis 的部署、主从与集群
老男孩Python全栈6期——redis--------------------------Linux 操作系统 默认的内存管理机制RSS:page cache:anno page:Linux操作系统 ...
- Leetcode 记录(101~200)
Now, I want to just use English to explain the problem, it's about two month before the interview, s ...
- 网络 [HNOI2016]
Description 一个简单的网络系统可以被描述成一棵无根树.每个节点为一个服务器.连接服务器与服务器的数据线则看做一条树边.两个服务器进行数据的交互时,数据会经过连接这两个服务器的路径上的所有服 ...