美文网首页
nginx 一些事儿

nginx 一些事儿

作者: 胡乱唱歌ing | 来源:发表于2018-12-05 09:45 被阅读0次

1.设置php脚本执行的有效时间

简单描述下一个完整的http请求到PHP处理的流程
客户端发起http请求,nginx收到请求后通过fastcgi协议把请求转发给php-fpm处理,php-fpm把请求分配给一个空闲的php进程处理业务逻辑,最终返回给nginx,由nginx返回给客户端。php-fpm是管理PHP进程的,因此,配置php执行有效时间需要对php-fpm进行配置,还有配置nginx的fastcgi的超时时间。
注意:所以单单在php程序里面设置set_time_limit是无效的
以下是一个完整配置php脚本执行有效时间的过程

1.1 修改nginx 的配置文件 location ~ .php(.*)$ {} 添以一下代码

fastcgi_connect_timeout 300;
fastcgi_read_timeout 300;
fastcgi_send_timeout 300;

1.2修改php-fpm配置文件

 request_terminate_timeout = 300s

2.nginx配置文件目录的权限,提高安全性

很多时候我们的程序都是通过git/svn进行版本控制的,所以程序的目录中通常会出现一些.svn .git这些隐藏的文件 这些文件是危险的说不定会被黑客所利用进而威胁到网站的安全性,还有就是我们的模版文件也必须是通过控制器才能加载的,但是如果用户直接访问模版文件绕过控制器呢?解决这些安全隐患可通过nginx配置实现

#禁止访问.svn .git .cvs
location ~ .*.(svn|git|cvs) {
    deny all;
}

#禁止访问视图文件,必须通过控制器才能加载视图
location ~ /themes/default/views 
{
    return 404;
}

3.防盗链

location ~* \.(gif|jpg|png|jpeg)$ {
    expires     30d;
    valid_referers none blocke  *.baidu.com *.google.com; www.test.com
    if ($invalid_referer) {
    rewrite ^/ http://www.test.com/404.jpg;
    #return 404;
    }
}

4.重定向rewrite 可解决跨域问题

rewrite ^/test/(.*) http://xn.test.com/$1;

5.反向代理

#swoole 与ningx配合使用静态nginx处理,php转向swoole服务
location / {
    proxy_http_version 1.1;
    proxy_set_header Connection "keep-alive";
    proxy_set_header X-Real-IP $remote_addr;
    if (!-e $request_filename) {
         proxy_pass http://192.168.233.129:9501;
    }
}

#websocket反向代理
location /wss {
   proxy_pass https://websocket;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "Upgrade";
}
upstream websocket {
    server 127.0.0.1:8888;
}

6.反向代理与负载均衡

#定义上游服务器,并且设置超时时间
upstream service_com {
     server 172.16.233.1:80 max_fails=2 fail_timeout=10s  weight=10; 
     server 127.0.0.1:8080 max_fails=2 fail_timeout=10s  weight=10; 
    # ip_hash; 是否使用IPhash
 }
server{
   listen 80;
   server_name xn.test.com;
   location / {
                 proxy_connect_timeout 5s;
                 proxy_read_timeout 5s;
                 proxy_send_timeout 5s;
                 proxy_next_upstream error timeout;
                 proxy_next_upstream_timeout 10s;
                 proxy_next_upstream_tries 2;
                 proxy_pass http://service_com;
                 add_header upstream_addr $upstream_addr;
                 proxy_set_header Host $host;
                 proxy_set_header X-Real-IP $remote_addr;
                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         }
}

server
{
    listen 8080;
    #listen [::]:80 default_server ipv6only=on;
    server_name xn.test.com;
    index index.html index.htm index.php;
    root  /home/wwwroot/xn.test.com;

    #error_page   404   /404.html;

    # Deny access to PHP files in specific directory
    #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

    include enable-php.conf;

    location /nginx_status
    {
        stub_status on;
        access_log   off;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
    }

    location ~ /.well-known {
        allow all;
    }

    location ~ /\.
    {
        deny all;
    }

    access_log  /home/wwwlogs/access.log;
}

7.nginx流量控制

https://legolasng.github.io/2017/08/27/nginx-rate-limiting/#%E9%AB%98%E7%BA%A7%E9%85%8D%E7%BD%AE%E7%A4%BA%E4%BE%8B

相关文章

  • nginx 一些事儿

    1.设置php脚本执行的有效时间 简单描述下一个完整的http请求到PHP处理的流程客户端发起http请求,ngi...

  • nginx

    目录 一些好的文档 Nginx官网Nginx中文文档Nginx热点微博 基本模块 一些案例

  • nginx的安装与配置

    nginx的安装: 安装Nginx之前需要先安装一些依赖和lib库: 直接安装Nginx: 检查nginx是否安装...

  • 【Nginx那点事儿】之 ①【 Nginx入门】

    一、Nginx为什么受青睐 ​ 在介绍Nginx具体的安装、配置以及原理之前先聊聊概念常识问题。那就是目前为什么...

  • nginx的基础应用

    nginx的基础应用 一、简介 今天我们将介绍一些nginx的简单应用,启动、停止nginx,重载nginx的配置...

  • CentOS7下安装Nginx

    CentOS7下安装Nginx 一.准备环境 首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之...

  • PHP-Ngnix环境搭建

    本文涉及PHP开发中一些环境搭建,如Larval和Ngnix。 Nginx Nginx启动关闭命令 Nginx s...

  • 高并发下的 Nginx 优化

    我已经谈过一些关于Nginx的常见问题; 其中有一些是关于如何优化Nginx. 很多Nginx新用户是从Apach...

  • 2,nginx的简单配置

    这是nginx配置文件中的一些简单信息,nginx.conf文件 user nginx; error_log /v...

  • 了解 Nginx 的基本概念

    前言 本篇是我学习Nginx的一些笔记,主要内容讲述了一些了解Nginx需要的基本概念。然后探讨一下Nginx的模...

网友评论

      本文标题:nginx 一些事儿

      本文链接:https://www.haomeiwen.com/subject/cjxicqtx.html