nginx 总结

本文详细介绍了Nginx的配置,包括设置上传文件限制为200M,开启文件压缩,配置HTTPS证书,支持WebSocket协议,实现重定向以及隐藏服务器版本号的方法。同时,也提及了PHP的相关设置,如post_max_size和max_execution_time等参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、常用的nginx 配置

二、nginx 上传文件限制

1.nginx 设置

2.php 设置

三、文件压缩

四、https 证书配置

五、https websocket

六、重定向

1.php 配置

2.nginx 配置

七、隐藏版本号

八、获取真实IP

1.nginx设置

2.扩展 gateway worker获取真实ip


一、常用的nginx 配置

server {

        listen 443 ssl;

        server_name www.kkk.com;

        root "";

         client_max_body_size 200m; 

        server_tokens off;

        gzip on;

        gzip_min_length 4k;

        gzip_buffers 16 8k;

        gzip_http_version 1.1;

        gzip_comp_level 2;

        gzip_types text/plain application/x-javascript application/javascript text/javascript         text/css application/xml application/x-httpd-php image/jpeg image/gif image/png         application/vnd.ms-fontobject font/x-woff font/ttf;

        gzip_vary on;

        location / {

                if (!-e $request_filename) {

                        rewrite ^(.*)$ /index.php/$1 last;

                        break;

                }

                index home.php index.html index.htm index.php l.php;

                error_page 405 =200 http://$host$request_uri;

                autoindex off;

        }

        location /aaaa{

                                 internal;

                                 alias G:/; 

        }

        location ~ \.php(.*)$ {

                fastcgi_pass 127.0.0.1:9000;

                fastcgi_index index.php;

                fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;

                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

                fastcgi_param PATH_INFO $fastcgi_path_info;

                fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

                include fastcgi_params;

        }

        location /wss/ {# https websocket

                proxy_pass http://127.0.0.1:8080;

                proxy_http_version 1.1;

                proxy_set_header Upgrade $http_upgrade;

                proxy_set_header Connection "Upgrade";

                proxy_set_header X-Real-IP $remote_addr;

        }

        ssl_certificate a.pem;

        ssl_certificate_key a.key;

        ssl_session_timeout 5m;

        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

        ssl_prefer_server_ciphers on;

    }

        server {

                listen 80;

                server_name www.kkk.com;

                rewrite ^(.*)$ https://$host$1 permanent;

        }

二、nginx 上传文件限制

1.nginx 设置

#写在http{}里面

client_max_body_size 200m; # nginx 上传文件限制

2.php 设置

post_max_size = 500M #post 提交时的大小限制,这里设置为500M。

upload_max_filesize = 500M #文件上传的最大限制。

max_input_time = 600 #最大输入时间,是指脚本解析输入数据(类似 POST 和 GET)允许的最大时间

max_execution_time = 300 #最大执行时间,是指php上传脚本的最大执行时间

三、文件压缩

     #写在http{}里面

         gzip on; # 开启gzip压缩
       gzip_min_length 4k; # 小于4k的文件不会被压缩,大于4k的文件才会去压缩
       gzip_buffers 16 8k; # 处理请求压缩的缓冲区数量和大小,比如8k为单位申请16倍内存空间;使用默认即可,不用修改
       gzip_http_version 1.1; # 早期版本http不支持,指定默认兼容,不用修改
       gzip_comp_level 2; # gzip 压缩级别,1-9,理论上数字越大压缩的越好,也越占用CPU时间。实际上超过2的再压缩,只能压缩一点点了,但是cpu确是有点浪费。因为2就够用了
       gzip_types text/plain application/x-javascript application/javascript text/javascript text/css application/xml application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/x-woff font/ttf;
      gzip_vary on; # 是否在http header中添加Vary: Accept-Encoding,一般情况下建议开启  

四、https 证书配置

#写在http{}里面

ssl_certificate a.pem;

 ssl_certificate_key a.key;

 ssl_session_timeout 5m;

 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;

 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

 ssl_prefer_server_ciphers on;

五、https websocket

#写在http{}里面

 location /wss/ {#  websocket

                proxy_pass http://127.0.0.1:8080;

                proxy_http_version 1.1;

                proxy_set_header Upgrade $http_upgrade;

                proxy_set_header Connection "Upgrade";

        }

https请求

wss://域名/wss/ 

ip请求

ws://8.130.44.1/wss/

六、重定向

这个我在弄php nginx  html5 video 视频进度条无法滑动,注意到这个配置

1.php 配置

set_time_limit(0);

if(isset($_GET['path'])){

        $name=$_GET['path'];

        header( 'Content-type: application/octet-stream' );

         header( 'Content-Disposition: attachment; filename="' . basename ( $name ) . '"' );         header( "Content-Length: " . filesize ( $name ));

        header( 'X-Accel-Redirect:/aaaa'.mb_substr($name,2,null,'utf8'));       

          //X-Accel-Redirect这个请求头告诉nginx 我要重定向,其中我的重定向标识是aaaa,         //nginx 匹配到aaaa 就访问某个路径的视频        

         //比如我的视频路径是G:/video/电影/杀破狼.mp4;        

         //那么这里写 //header( 'X-Accel-Redirect:/aaaa/video/电影/杀破狼.mp4'); nginx 就会在G盘里面找

}

2.nginx 配置

#写在http{}里面

location /aaaa{

        internal;

        alias G:/; #跟上面的php呼应

}

七、隐藏版本号

#写在 http{}里面

  server_tokens off;

#php 隐藏X-Powered-By

修改 php.ini 文件。添加或修改 expose_php = Off

八、获取真实IP

1.nginx设置

location /wss/ {# websocket

                proxy_pass http://127.0.0.1:8080;

                proxy_http_version 1.1;

                proxy_set_header Upgrade $http_upgrade;

                proxy_set_header Connection "Upgrade";

                proxy_set_header X-Real-IP $remote_addr;

        }

2.扩展 gateway worker获取真实ip

start_gateway2.php

$gateway->onConnect = function($connection)
{
    $connection->onWebSocketConnect = function($connection , $http_header)
    {
        $_SERVER['realIp']=$_SERVER['HTTP_X_REAL_IP'];
    };
};

Events.php

public static function onMessage($client_id, $message)
    {
                var_dump($_SESSION);

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李修缘999

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值