设为首页 收藏本站
查看: 529|回复: 0

[经验分享] 【Nginx】06、LNMP-xiexiaojun

[复制链接]

尚未签到

发表于 2018-11-10 08:35:43 | 显示全部楼层 |阅读模式
  一、nginx代理分离设置
  1、动态网页
  在服务器端执行,根据请求的不同生成不同的结果,并将结果返回给请求者,就叫动态网页。
  开发动态网页的常用语言有及nginx与其结合的模块:
  php:fastcgi,php不能做为nginx的模块运行
  jsp(tomcat):http协议直接代理
  python:uwsgi
  perl:cgi
  2、图片和其它文件分离
location / {  proxy_pass http://192.168.100.179;
  }
  location ~* \.(jpg|jpeg|png|gif)$ {
  proxy_pass http://192.168.100.175;
  }
  3、php文件和其它文件分离
location / {  proxy_pass http://192.168.100.179;
  }
  location ~* \.php$ {
  fastcgi_pass http://xxx;
  }
  二、LNMP
  1、环境
  Node1:php-fpm 7.1.1
  Node2:Mariadb 10.0.20
  Node7:nginx 1.10.3
  各安装过程可以查看之前的文章,不重复了
  一般建议将静态web服务器和动态web应用服务器放在同一台服务器上,放在不同服务器上时,需要很大的网络开销
  2、整合nginx和php
  
  源码编译安装的nginx配置文件中有默认就有整合php的配置项(被注销了)
# proxy the PHP scripts to Apache listening on 127.0.0.1:80  
        ###  php作为httpd的模块工作
  
        #location ~ \.php$ {
  
        #    proxy_pass   http://127.0.0.1;
  
        #}
  

  
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  
        ### php-fpm服务器
  
        #location ~ \.php$ {
  
        #    root           html;      # php-fpm服务器上放动态页面的目录
  
        #    fastcgi_pass   127.0.0.1:9000;
  
        #    fastcgi_index  index.php;
  
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  
        #    include        fastcgi_params;
  
        #}
  fastcgi模块的常用指令:
  fastcgi_pass
  指定fastcgi服务监听端口、地址、也支持使用 Uxin socket
  ngx_http_fastcgi_module
Syntax:fastcgi_pass address;Default:—Context:location, if in location  Sets the address of a FastCGI server. The address can be specified as a domain name or IP address, and a port:
fastcgi_pass localhost:9000;
  or as a UNIX-domain socket path:
fastcgi_pass unix:/tmp/fastcgi.socket;
  fastcgi_param
  定义传递给fpm的参数
Syntax:fastcgi_param parameter value [if_not_empty];Default:—Context:http, server, location  Sets a parameter that should be passed to the FastCGI server. The value can contain text, variables, and their combination. These directives are inherited from the previous level if and only if there are no fastcgi_param directives defined on the current level.
  The following example shows the minimum required settings for PHP:
fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;  
fastcgi_param QUERY_STRING    $query_string;
  fastcgi_bind
  指定连接fastcgi服务时使用的地址
  fastcgi_index
  定义默认主页
  实例:
  修改nginx的配置文件
server {  
    root /www/c.net;
  
    server_name www.c.net;
  
#    location / {
  
#        proxy_pass http://backend;
  
#        health_check;
  
#          }
  
        location ~ \.php$ {
  
            root /data/htdocs2;
  
            fastcgi_pass   192.168.10.1:9000;
  
            fastcgi_index  index.php;
  
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  
            include        fastcgi_params;
  
                 }
  
      }
  源码编译安装的nginx生成的fastcgi_params文件格式有问题,我们重新提供一个配置文件:
[root@Node7 nginx]# cat fastcgi_params  
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
  
fastcgi_param  SERVER_SOFTWARE    nginx;
  
fastcgi_param  QUERY_STRING       $query_string;
  
fastcgi_param  REQUEST_METHOD     $request_method;
  
fastcgi_param  CONTENT_TYPE       $content_type;
  
fastcgi_param  CONTENT_LENGTH     $content_length;
  
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
  
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
  
fastcgi_param  REQUEST_URI        $request_uri;
  
fastcgi_param  DOCUMENT_URI       $document_uri;
  
fastcgi_param  DOCUMENT_ROOT      $document_root;
  
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
  
fastcgi_param  REMOTE_ADDR        $remote_addr;
  
fastcgi_param  REMOTE_PORT        $remote_port;
  
fastcgi_param  SERVER_ADDR        $server_addr;
  
fastcgi_param  SERVER_PORT        $server_port;
  
fastcgi_param  SERVER_NAME        $server_name;
  在php-fpm服务器上提供测试页面:
[root@Node1 htdocs2]# cat index.php  

  测试:
DSC0000.png

  fastcgi模块常用的指令:
  fastcgi_pass:指定fastcgi服务监听端口、地址、也支持使用 Uxin socket
  fastcgi_bind:指定练习fpm服务时使用的地址
  fastcgi_param:定义传递给fpm的参数
  fastcgi_index:php的主页面文件
  php-fpm的结果也是可以缓存的,缓存空间使用proxy_cache_path定义,使用fastcg_cache定义
  fastcgi_cache_path
  fastcgi_cache
  fastcgi_cache_vaild
  fastcgi_connect_timeout:连接fastcgi服务器的超时时长
  fastcgi_send_timeout:向fastcgi服务器传输数据的超时时长
  用法和proxy模块上的相应指令一样,不再重复
Embedded Variables
  The ngx_http_fastcgi_module module supports embedded variables that can be used to set parameters using the fastcgi_param directive:
  $fastcgi_script_name
  保存了请求报文中的uri
  For example, for the “/info/” request with the following directives
fastcgi_index index.php;  
fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
  the SCRIPT_FILENAME parameter will be equal to“/home/www/scripts/php/info/index.php”
  # 如果前面没有使用root指令fastcgi服务器上的根目录,这里就要使用绝对路径
  三、获取php-fpm的状态的状态信息
server {  
    root /www/c.net;
  
    server_name www.c.net;
  
#    location / {
  
#        proxy_pass http://backend;
  
#        health_check;
  
#          }
  
        location ~* \.php$|status|ping {
  
                     # 要先上php-fpm的配置文件中启用status,ping选项
  
            root /data/htdocs2;
  
            fastcgi_pass   192.168.10.1:9000;
  
            fastcgi_index  index.php;
  
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  
            include        fastcgi_params;
  
                 }
  
      }
  测试:
DSC0001.png

  
  php-fpm状态信息详解:
  pool:          # 进程池名称
  process manager:   # 进程管理;有两种类型static和dynamic
  start time:      # 系统启动的时间
  start since:      # 已运行时长
  accepted conn:     # 已经接收的请求数
  listen queue:        # 监听(等待)队列中的请求个数
  max listen queue:     # fpm启动以来,监听(等待)队列中的请求的总数
  listen queue len:     # 等待队列长度
  idle processes:      # 空闲的进程数
  active processes:     # 活动的进程数
  total processes:      # 总进程数
  max active processes:   # fpm启动以来,最大活动进程数
  max children reached:   # fpm启动以来,达到最大子进程的次数
  slow requests:       # 慢速请求的个数



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-633069-1-1.html 上篇帖子: 使用nginx+lua实现WAF功能 下篇帖子: nginx基础应用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表