项目01-nginx模块

1、nginx介绍

nginx是一款高性能web服务器和反向代理服务器,在互联网项目中使用非常频繁,尤其其出色的性能以及轻量级进程占用,已经超过了apache的httpd服务器的使用量。内部可以配置零拷贝实现快速文件传输。

2、openresty

openresty是将nginx现有一些重要插件做了集成,省去安装nginx之后还需要在安装插件的繁琐步骤,内置luajit插件,能解决接受post提交请求、json消息体解析等功能。

3、安装openresty

3.1 windows下安装

下载软件包D:\downloads\openresty-1.13.6.1-win32.zip,解压即可。

3.2 linux下安装

  1. 添加openresty yum源

    创建/etc/yum.repos.d/openresty.repo文件,内容如下:

    [openresty]
    name=Official OpenResty Open Source Repository for CentOS
    baseurl=https://openresty.org/package/centos/$releasever/$basearch
    skip_if_unavailable=False
    gpgcheck=1
    repo_gpgcheck=1
    gpgkey=https://openresty.org/package/pubkey.gpg
    enabled=1
    enabled_metadata=1
  2. 清空yum缓存并重建缓存

    #切换到root账户下操作
    $>su root
    $>yum clean all
    $>yum makecache
  3. 通过yum进行安装

    #搜索openresty软件包
    $>sudo yum cache search openresty
    $>sudo yum install openresty

4、配置反向代理服务器

配置文件为openresty-1.13.6.1-win32\conf\nginx.conf


#user nobody;
worker_processes 1; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; # log_format main escape=json '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'
# '"$request_body"'
# '"$http_client_time"';
log_format main escape=json $remote_addr#$http_client_time#$time_local#$status#$request_body; access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on;
##############################################################
######## 该部分为反向代理 ###############
##############################################################
upstream servers{
server s101:80 weight=1;
server s102:80 weight=1;
} map $http_x_forwarded_for $clientRealIp {
~^(?P<firstAddr>[0-9\.]+),?.*$ $firstAddr;
} server {
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main;
#
underscores_in_headers on; location / {
#root html;
#index index.html index.htm;
error_page 405 =200 $1;
lua_need_request_body on;
content_by_lua 'local s = ngx.var.request_body'; proxy_pass http://servers;
proxy_set_header Host $host;
proxy_set_header remove_user_ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#}
}

5、配置centos上的nginx web服务器

/etc/nginx/nginx.conf

#user  nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream; log_format main escape=json $msec#$time_local#$clientRealIp#$http_client_time#$status#$request_body;
access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; map $http_x_forwarded_for $clientRealIp {
~^(?P<firstAddr>[0-9\.]+),?.*$ $firstAddr;
} server {
listen 8888;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main;
underscores_in_headers on; location / {
root html;
index index.html index.htm;
error_page 405 =200 $1;
lua_need_request_body on;
content_by_lua 'local s = ngx.var.request_body';
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
}
}

6、nginx管理命令

6.1 启动openresty

$>su root				#切换到root账户
$>openresty #启动命令

6.2 停止命令

$>su root
$>openresty -s stop #停止命令

6.3 重新加载配置文件

$>su root
$>openresty -s reload #重新加载

6.4 检查配置文件正确性

$>su root
$>openresty -t #检查文件

6.5 查看帮助

#查看帮助
$>sudo openresty -h
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives] Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/openresty/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file $>sudo openresty -s reload | stop | quit |reopen
$>sudo openresty -t

7、注意事项

7.1 关闭centos的防火墙与开启自启

$>sudo service firewalld status		#查看防火墙
$>sudo service firewalld start #启动防火墙
$>sudo service firewalld stop #停止后防火墙 $>sudo chkconfig firewalld off #关闭开机自启
$>sudo chkconfig firewalld on #启动开机自启
$>sudo chkconfig #查看开机自启项列表

7.2 不要使用浏览器访问nginx

我们配置的nginx服务器接受post方式访问,浏览器是get方式,因此无法访问nginx服务器,可以在chrome中安装poster插件实现发送post请求。

项目01-nginx模块的更多相关文章

  1. day64:nginx模块之限制连接&状态监控&Location/用nginx+php跑项目/扩展应用节点

    目录 1.nginx模块:限制连接 limit_conn 2.nginx模块:状态监控 stub_status 3.nginx模块:Location 4.用nginx+php跑wordpress项目 ...

  2. Nginx模块fastcgi_cache的几个注意点 转

    Nginx模块fastcgi_cache的几个注意点   去年年底,我对nginx的fastcgi_cache进行摸索使用.在我的测试过程中,发现一些wiki以及网络上没被提到的注意点,这里分享一下. ...

  3. 开发Nginx模块

    开发Nginx模块 前面的哪些话 关于Nginx模块开发的博客资料,网上很多,很多.但是,每篇博客都只提要点,无法"step by step"照着做,对于初次接触Nginx开发的同 ...

  4. Nginx模块

    模块概述 https://kb.cnblogs.com/page/98352/ Nginx模块工作原理概述 (Nginx本身支持多种模块,如HTTP模块.EVENT模块和MAIL模块,本文只讨论HTT ...

  5. 若依项目利用nginx实现负载均衡及保持会话

    记录一下若依项目利用nginx实现负载均衡及保持会话的步骤. 此次作为试验性的测试,为了方便在本地window的环境上实现. 具体步骤: 1.安装两个tomcat8,可以下载一个后,另一个复制即可,下 ...

  6. nginx源代码分析--nginx模块解析

    nginx的模块很之多.能够觉得全部代码都是以模块的形式组织.这包含核心模块和功能模块,针对不同的应用场合.并不是全部的功能模块都要被用到,附录A给出的是默认configure(即简单的httpser ...

  7. 通过apache,和nginx模块去除html中的空格和tab

    最近一个项目中,合作方要求去除html中的空格,不想改代码,所以百度了一下通过apache,和nginx模块去除html中的空格和tab的方案,下面记录下来: 一.nginx nginx可以通过mod ...

  8. OpenResty / Nginx模块,Lua库和相关资源的列表

    OpenResty / Nginx模块,Lua库和相关资源的列表 什么是OpenResty OpenResty是一个成熟的网络平台,它集成了标准的Nginx核心,LuaJIT,许多精心编写的Lua库, ...

  9. 手把手教你开发Nginx模块

    前面的哪些话 关于Nginx模块开发的博客资料,网上很多,很多.但是,每篇博客都只提要点,无法"step by step"照着做,对于初次接触Nginx开发的同学,只能像只盲目的蚂 ...

  10. day08 Nginx模块

    day08 Nginx模块 lnmp架构 l :Linux n :Nginx m :MySQL p :Python/PHP lnmp架构:是最简单的架构 Nginx中的模块(Python模块):前提是 ...

随机推荐

  1. 记录下自己安装cuda以及cudnn

    之前已经装过一次了,不过没有做记录,现在又要翻一堆博客安装,长点记性,自己记录下. 环境 ubuntu16.04 python2.7 商家送过来时候已经装好了显卡驱动,所以省去了一大麻烦. 剩下的就是 ...

  2. POJ1052 Plato's Blocks

    题目来源:http://poj.org/problem?id=1052 题目大意: 把1*1*1的小立方体通过粘接相邻面组成大的立方体的形状.如下图所示: 一层一层地堆叠,立方体从三个方向的投影会分别 ...

  3. mysql 备份时间 %date~0,4%和 %time~0,2%等用法详解

    比如在windowscmd命令行窗口执行date命令后这个环境变量的值为 当前日期:2014-09-01 星期六 或2014/09/01 周六 那么如下的各个操作的意义如下:%date:~0,4% 表 ...

  4. SQL全文搜索引擎 Sphinx

    Sphinx是一个基于SQL的全文检索引擎,可以结合MySQL,PostgreSQL做全文搜索, 它可以提供比数据库本身更专业的搜索功能,使得应用程序更容易实现专业化的全文检索.   Sphinx特别 ...

  5. IP 地址分类

    1.1 网络IP地址分类 网络通讯过程中数据封装与解封过程(网际互联通讯过程) TCP/IP模型 1)应用层 总结记录一些常见网络协议以及对应的端口号(FTP HTTP telnet) 2)主机到主机 ...

  6. Tomcat常见问题

    1. tomcat主页 http://localhost:8080 打不开 设置环境变量JAVA_HOME,确认端口为8080,查看webapps\ROOT文件夹是否存在 2. 访问tomcat管理页 ...

  7. 经典网络LeNet5看卷积神经网络各层的维度变化

    本文介绍以下几个CNN经典模型:Lenet(1986年).Alexnet(2012年).GoogleNet(2014年).VGG(2014年).Deep Residual Learning(2015年 ...

  8. sf04_操作系统中 heap 和 stack 的区别

    概述 本文分三部分,描述有所重叠,但可以让你对栈与堆有一个比较清晰.全面的认识 heap 和 stack是什么 堆栈是两种数据结构.堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top) ...

  9. py---------面向对象进阶

    一.isinstance 和 issubclass isinstance(obj,cls)检查obj是否是类cls的对象,是则返回True class Foo(object): pass obj = ...

  10. SpringMVC中的视图和视图解析器

    对于控制器的目标方法,无论其返回值是String.View.ModelMap或是ModelAndView,SpringMVC都会在内部将它们封装为一个ModelAndView对象进行返回.  Spri ...