【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登录服务器.很是纳闷,如下所示: ...
随机推荐
- Linux安装Elasticsearch
本文介绍Linux环境如何安装Elasticsearch. 本文环境是在腾讯云服务器CentOS7.2搭建的,JDK1.8,elasticsearch-5.4.2. 1 安装JDK 网上教程很多,也可 ...
- YOLO系列:YOLO v3解析
本文好多内容转载自 https://blog.csdn.net/leviopku/article/details/82660381 yolo_v3 提供替换backbone.要想性能牛叉,backbo ...
- 杭电1024----Max Sum Plus Plus
/* 这题还没有理解透彻.某个dalao也不写注释.只能自己理解了... 先求为i个元素(1<=i<=M)为一个区间的最大和,保证元素个数大于等于i个,递推到M个即可 借鉴原址:http: ...
- 在Node.js使用Promise的方式操作Mysql(续)
在之后的开发中,为了做一些事务开发,我把mysql的连接代码从之前的query函数中分离出来了,直接使用原生的方法进行操作,但发现还是有点问题 原因是原生的node-mysql采用了回调函数的方式,同 ...
- 洛谷.4245.[模板]任意模数NTT(MTT/三模数NTT)
题目链接 三模数\(NTT\): 就是多模数\(NTT\)最后\(CRT\)一下...下面两篇讲的都挺明白的. https://blog.csdn.net/kscla/article/details/ ...
- Python应用——自定义函数:分割PDF文件函数
案例 将一个 pdf 文件按要求分割为几个部分.比如说一个pdf有20页,分成5个pdf文件,每个pdf文件包含4页.设计函数实现? Python代码 from PyPDF2 import PdfFi ...
- 1099 Lottery
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1099 英文水平太差了,题目实在是不知道是什么意思,然后看了其他高手写的思路,才看明白. 题意,收集n张彩票 ...
- 2110 ACM Crisis of HDU 母函数
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2110 题意:分出1/3的价值,有几种可能? 思路:母函数 与之前的题目2079相似,复习笔记再来写代码: ...
- 1. Spring 简介以及关于 Eclipse 的 Spring Tool Suite 插件安装
今天开始学 Spring 了,就先来认识一下什么是 Spring 吧. 1. 首先,Spring 是一个框架,而且是开源的. 2. Spring 为简化企业级应用开发而生.使用 Spring 可以使简 ...
- Eclipse Maven pom.xml 警告No grammar constraints (DTD or XML schema)
消除警告方案: <?xml version="1.0" encoding="UTF-8" standalone="no"?> & ...