项目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请求。

最新文章

  1. android内部培训视频_第三节(3)_常用控件(ViewPager、日期时间相关、ListView)
  2. Appfuse:扩展自己的GenericManager
  3. js文件如何最后加载
  4. 总结:视频播放的四种实现方案(Native)
  5. 重走java---Step 1
  6. Linux 修改主机名 和 ip 映射关系
  7. Eclipse快捷键及各种设置(转载)
  8. 开启Objective-C --- OC基础知识
  9. 115 Java Interview Questions and Answers – The ULTIMATE List--reference
  10. 【Pure】
  11. MVC中的过滤器
  12. 开机启动遇到grub rescue,无法启动系统解决方法
  13. SQL Server Alwayson读写分离配置
  14. Xcode部分快捷键
  15. HighCharts之2D带有Legend的饼图
  16. (译文)学习ES6非常棒的特性-深入研究var, let and const
  17. shell基础之bash
  18. python之抽象类
  19. linux每日命令(30):Linux 用户及用户组相关文件、命令详解
  20. 转:JVM 内存初学 (堆(heap)、栈(stack)和方法区(method) )

热门文章

  1. appium键盘处理
  2. 查看php 相关信息
  3. UVA1714 Keyboarding
  4. django ORM 连表查询2
  5. 阿里云ECS服务器FileZilla&#39;被动模式失败&#39;的处理办法
  6. 事务背书 ACID, CAP, BASE
  7. 在vue2.x中安装sass并配置
  8. Zookeeper---初识
  9. 可输入的 Combox(DropDownList)
  10. Linux 文件锁flock 实现两个进程相互监听存活状态