加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 正文

优秀的 Nginx 极简教程,覆盖了常用场景

发布时间:2021-05-27 11:05:45 所属栏目:创业 来源:互联网
导读:nginx 的行使较量简朴,就是几条呼吁。 常用到的呼吁如下: nginx-sstop快速封锁Nginx,也许不生涯相干信息,并敏捷终止web处事。 nginx-squit安稳封锁Nginx,保

nginx -s reopen     从头打开日记文件。 

nginx -c filename   为 Nginx 指定一个设置文件,来取代缺省的。 

nginx -t            不运行,而仅仅测试设置文件。nginx 将搜查设置文件的语法的正确性,并实行打开设置文件中所引用到的文件。 

nginx -v            表现 nginx 的版本。 

nginx -V            表现 nginx 的版本,编译器版本和设置参数。 

假如不想每次都敲呼吁,可以在 nginx 安装目次下新添一个启动批处理赏罚文件startup.bat,双击即可运行。内容如下:

@echo off 

rem 假如启动前已经启动nginx并记录下pid文件,会kill指定历程 

nginx.exe -s stop 

 

rem 测试设置文件语法正确性 

nginx.exe -t -c conf/nginx.conf 

 

rem 表现版本信息 

nginx.exe -v 

 

rem 凭证指定设置去启动nginx 

nginx.exe -c conf/nginx.conf 

假如是运行在 Linux 下,写一个 shell 剧本,大同小异。

nginx 设置拭魅战

我始终以为,各类开拓器材的设置照旧团结拭魅战来报告,会让人更易领略。

我们先实现一个小方针:不思量伟大的设置,仅仅是完成一个 http 反向署理。

nginx.conf 设置文件如下:

注:conf / nginx.conf 是 nginx 的默认设置文件。你也可以行使 nginx -c 指定你的设置文件

#运行用户 

 

#user somebody; 

 

#启动历程,凡是配置成和cpu的数目相称 

 

worker_processes 1; 

 

#全局错误日记 

 

error_log D:/Tools/nginx-1.10.1/logs/error.log; 

 

error_log D:/Tools/nginx-1.10.1/logs/notice.log notice; 

 

error_log D:/Tools/nginx-1.10.1/logs/info.log info; 

 

#PID文件,记录当前启动的nginx的历程ID 

 

pid D:/Tools/nginx-1.10.1/logs/nginx.pid; 

 

#事变模式及毗连数上限 

 

events { 

 

worker_connections 1024; #单个靠山worker process历程的最大并发链接数 

 

 

#设定http处事器,操作它的反向署理成果提供负载平衡支持 

 

http { 

 

#设定mime范例(邮件支持范例),范例由mime.types文件界说 

 

include D:/Tools/nginx-1.10.1/conf/mime.types; 

 

default_type application/octet-stream; 

 

#设定日记 

 

log_format main '[$remote_addr] - [$remote_user] [$time_local] "$request" ' 

 

'$status $body_bytes_sent "$http_referer" ' 

 

'"$http_user_agent" "$http_x_forwarded_for"'; 

 

access_log D:/Tools/nginx-1.10.1/logs/access.log main; 

 

rewrite_log on; 

 

#sendfile 指令指定 nginx 是否挪用 sendfile 函数(zero copy 方法)来输出文件,对付平凡应用, 

 

#必需设为 on,假如用来举办下载等应用磁盘IO重负载应用,可配置为 off,以均衡磁盘与收集I/O处理赏罚速率,低落体系的uptime. 

 

sendfile on; 

 

#tcp_nopush on; 

 

#毗连超时时刻 

 

keepalive_timeout 120; 

 

tcp_nodelay on; 

 

#gzip压缩开关 

 

#gzip on; 

 

#设定现实的处事器列表 

 

upstream zp_server1{ 

 

server 127.0.0.1:8089; 

 

 

#HTTP处事器 

 

server { 

 

#监听80端口,80端口是知名端标语,用于HTTP协议 

 

listen 80; 

 

#界说行使会见 

 

server_name ; 

 

#首页 

 

index index.html 

 

#指向webapp的目次 

 

root D:1_WorkspaceProjectgithubzpSpringNotesspring-securityspring-shirosrcmainwebapp; 

 

#编码名目 

 

charset utf-8; 

 

#署理设置参数 

 

proxy_connect_timeout 180; 

 

proxy_send_timeout 180; 

 

proxy_read_timeout 180; 

 

proxy_set_header Host $host; 

 

proxy_set_header X-Forwarder-For $remote_addr; 

 

#反向署理的路径(和upstream绑定),location 后头配置映射的路径 

 

location / { 

 

proxy_pass http://zp_server1; 

 

 

#静态文件,nginx本身处理赏罚 

 

location ~ ^/(images|javascript|js|css|flash|media|static)/ { 

 

root D:1_WorkspaceProjectgithubzpSpringNotesspring-securityspring-shirosrcmainwebappviews; 

 

#逾期30天,静态文件不怎么更新,逾期可以设大一点,假如频仍更新,则可以配置得小一点。 

 

expires 30d; 

 

 

#设定查察Nginx状态的地点 

 

location /NginxStatus { 

 

stub_status on; 

 

access_log on; 

 

auth_basic "NginxStatus"; 

 

auth_basic_user_file conf/htpasswd; 

 

 

#榨取会见 .htxxx 文件 

 

location ~ /.ht { 

 

deny all; 

 

 

#错误处理赏罚页面(可选择性设置) 

 

#error_page 404 /404.html; 

 

#error_page 500 502 503 504 /50x.html; 

 

#location = /50x.html { 

 

# root html; 

 

#} 

 

 

 

好了,让我们来试试吧: 

 

启动 webapp,留意启动绑定的端 

好了,让我们来试试吧:

启动 webapp,留意启动绑定的端口要和 nginx 中的 upstream 配置的端口保持同等。

变动 host:在 C:WindowsSystem32driversetc 目次下的 host 文件中添加一条 DNS 记录

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读