【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登录服务器.很是纳闷,如下所示: ...
随机推荐
- C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥
C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...
- 2、SQL UNION 和 UNION ALL 操作符
网址:http://www.w3school.com.cn/sql/sql_union.asp SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意, ...
- RESTful restful api Representational State Transfer
通俗直白讲:REST是一种编写风格,一种API接口规范.它的风格就是将对象(如学生)的状态(如增删改查,API接口版本号等等)通过其他方式传递,API的接口地址突显出描述的对象. -- == REST ...
- C#常用字符串函数
Compare 比较字符串的内容,考虑文化背景(场所),确定某些字符是否相等 CompareOrdinal 与Compare一样,但不考虑文化背景 Format 格式化包含各种值的字符串和如何格式化每 ...
- C#winform自定义控件大全
对C# WinForm开发系列收集的控件使用方面进行整理, 加入了一些文章, 不断补充充实, 完善这方面. 基础 - 常用控件 C# WinForm开发系列 - CheckBox/Button/Lab ...
- Java并发编程(十四)-- 线程池实现原理
在上一章我们从宏观上介绍了ThreadPoolExecutor,本文将深入解析一下线程池的具体实现原理 原理解析 线程池状态 在ThreadPoolExecutor中定义了一个volatile变量,另 ...
- Android应用程序性能优化Tips
对于我们设计的应用需要做到以下特征:build an app that's smooth, responsive(反应敏捷), and uses as little battery as possib ...
- MySQL JDBC简单使用
首先需要去MySQL官网下载MySQL JDBC驱动 导入jar包 String driver = "com.mysql.jdbc.Driver"; String url = &q ...
- Node_初步了解(3)回调,作用域,上下文
1. //回调:回调是异步编程最基本的方法,node.js需要按顺序执行异步逻辑的时候,一般采用后续传递的方式,将后续逻辑封装在回调函数中,作为起始函数的参数. //具名函数 function lea ...
- 29、粘包现象(struct模块)
昨天我们所做的套接字是有漏洞的,它会出现粘包现象,没有发现这个问题的我们今天会进行演示.今天也会稍微讲解一下基于udp的套接字. 本篇导航: 基于udp的套接字 粘包现象 粘包 解决粘包方法 stru ...