首页 > 开发 > Nginx > 正文

Nginx的使用经验小结

2020-07-28 15:57:52
字体:
来源:转载
供稿:网友
相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额。今天我们就简单介绍下本人在使用nginx的过程中的一些小小的经验

Nginx

Nginx简单介绍

一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器

Nginx命令参数

nginx -t 测试配置是否正确
nginx -s reload 加载最新配置
nginx -s stop 立即停止
nginx -s quit 优雅停止
nginx -s reopen 重新打开日志
kill -USR2 cat /usr/local/nginx/logs/nginx.pid 快速重启

Nginx全局段配置

worker_processes 1;工作进程为1个 CPU 数量 * 核数
events 区段 网卡请求 80 443 Nginx 触发事件

Nginx配置虚拟主机

listen
server_name
location

Nginx日志管理

* 系统默认日志格式:log_format main '$remote_addr $request_length $body_bytes_sent $request_time[s] - - [$time_local] ' '"$request" $status $http_referer "-" "$http_user_agent" $server_name $server_addr $http_x_forwarded_for $http_x_real_ip';
* 自定义日志格式:log_format simple '$remote_addr -- $request'
* 系统默认日志释义:远程IP-远程用户/用户时间 请求方法 请求body长度长度 referer 来源信息 http-user-agent 用户代理/蜘蛛 被转发请求的原始ip http_x_forwarded_for 在经过代理是 代理把你的本来的IP加在此头信息中,传输你的原始IP

Nginx-Laravel5 项目搭建

window上传本地项目使用 secureCRT-sftp put get 命令上传 connect sftp session, 远程:cd 本地:lcd,lpwd, put *.zip
Laravel5返回500权限修改:1.chmod 777 -R storage 2.chmod 777 -R bootstrap/cache/
配置env : vi .env 修改数据库配置相关参数
执行 php artisan migrate 创建数据表

Nginx 支持pathinfo

# 典型配置location ~ /.php$ {  root      html;  fastcgi_pass  127.0.0.1:9000;  fastcgi_index index.php;  fastcgi_param SCRIPT_FILENAME $DOCUMENT_ROOT$fastcgi_script_name;  include    fastcgi_params;}# 修改第1,6行,支持pathinfolocation ~ /.php(.*)$ { # 正则匹配.php后的pathinfo部分  root html;  fastcgi_pass  127.0.0.1:9000;  fastcgi_index index.php;  fastcgi_param SCRIPT_FILENAME $DOCUMENT_ROOT$fastcgi_script_name;  fastcgi_param PATH_INFO $1; # 把pathinfo部分赋给PATH_INFO变量  include    fastcgi_params;}

Nginx 支持URL重写

1.如果 不是资源文件就重写

if (!-e $request_filename) {  rewrite (.*)$ /index.php/$1;}

2.try_files

try_files $uri $uri/ /index.php?$args;

Nginx 反向代理与动静分离

用Nginx做反向代理用proxy_pass,以反向代理为例,nginx不自己处理图片的相关请求,而是把图片的请求转发给Apache处理。

location ~/.(jpg|jpeg|png|gif)${   proxy_pass HTTP://IP:port; }

反向代理导致了后端服务器接到客户端IP为前端服务器的IP而不是真正的IP解决方案

location ~ /.(jpg|jpeg|png|gif) {   proxy_set_header X-Forwarded-For $remote_addr;   proxy_pass IP:port; }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表