当前位置: 首页 > news >正文

nginx的配置文件概述及简单demo(二)

默认配置文件

当安装完nginx后,它的目录下通常有默认的配置文件


#user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {worker_connections  1024;
}http {include       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  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}}

可以看到主要分为3个部分

  • 全局块
    从开头到events块之间的内容,被称为全局块,从配置文件开始到 events 块之间的内容,主要会设置一些影响 nginx 服务整体运行的配指,主要包括配置运行 Nginx 服务器的用户(组)、允许生成的 worker process ,进程 PID 存放路径、日志存放路径和类型以及配置文件的引入等。

这里部分是全局块的内容,这里面除了注释,只有 “worker_processes 1;”,这样一条属性配置

#user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;

这是 Nginx 服务器并发处理服务的关健配置,worker_processes 值越大,可以支持的并发处理量也越多,但是受到硬件、软件等设备的制约

  • event块
events {worker_connections  1024;
}

events 块涉及的指今主要影响 Nginx 服务与用户的网络连接,常用的设置包括是否开启对多 work process下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word process 可以同时支持的最大连接数等。
上述例子就表示每个 work process 支持的最大连接数为 1024.这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活

  • http块
    这算是 Nginx 服务器配置中最频察的部分,代理、缓存和志定义等绝大多数功能和第三方模块的配置都在这里
    需要注意的是:http 块也可以包括 http 全局块、server 块
http {include       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  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}}

http块配置详解

http 块也可以包括 http 全局块、server 块

http全局块

http 全局块配置的指>包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等

    include       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  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;

server块

这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。每个 http 块可以包括多个 server 块,而每个 server 块就相当于一个虚拟主机。而每个 server 块也分为全局 server 块,以及可以同时包含多个 locaton 块。

 server {listen       80;server_name  localhost;location / {root   html;index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}

而实际上server块也可以接着细分下去,每个 server 块也分为全局 server 块,以及可以同时包含多个 locaton 块。
全局server块的内容

listen       80;
server_name  localhost;
error_page   500 502 503 504  /50x.html;

locaton 块内容

location / {root   html;index  index.html index.htm;}location = /50x.html {root   html;}

我们实际上要配置的内容都在http块中,给两个简单地例子

nginx简单demo

demo1

访问nginx转到tomcat页面,修改nginx配置,修改server_name,以及location / 模块的转发路径

worker_processes  1;
events {worker_connections  1024;
}
http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;server {listen       80;server_name  121.4.170.108;location / {proxy_pass  http://127.0.0.1:8080;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}}

这里我们监听了80端口,然后server_name是主机名或者ip,转发路径是我们tomcat的地址

demo2

部署多个tomcat,使用nginx做反向代理,部署在同一个服务器,根据路径转换到不同的服务器 ,127.0.0.1即代表本地,proxy_pass后面的地址代表我们转发的路径

worker_processes  1;
events {worker_connections  1024;
}
http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;server {listen       80;server_name  121.4.170.108;# 在页面输入121.4.170.108  会默认跳转到tomcat的页面location / {proxy_pass  http://127.0.0.1:8080;}# 第一个tomcat代理location ~ /a/ {proxy_pass   http://127.0.0.1:8080;}# 第一个tomcat的代理location ~ /b/ {proxy_pass   http://127.0.0.1:8081;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
}
http://www.lryc.cn/news/184298.html

相关文章:

  • Apollo Planning2.0决策规划算法代码详细解析 (2): vscode gdb单步调试环境搭建
  • flex 布局:元素/文字靠右
  • java基础-第1章-走进java世界
  • jvm 堆内存 栈内存 大小设置
  • 免杀对抗-反沙盒+反调试
  • QTimer类的使用方法
  • (三)行为模式:9、空对象模式(Null Object Pattern)(C++示例)
  • Django实战项目-学习任务系统-用户登录
  • 【动手学深度学习-Pytorch版】Transformer代码总结
  • 做外贸独立站选Shopify还是WordPress?
  • echarts的bug,在series里写tooltip,不起作用,要在全局先写tooltip:{}才起作用,如果在series里写的不起作用就写到全局里
  • jmeter分布式压测
  • consulmanage部署
  • 大数据软件项目的验收流程
  • 《第一行代码Andorid》阅读笔记-第一章
  • Educational Codeforces Round 146 (Rated for Div. 2)(VP)
  • 9.30国庆
  • java基础-第4章-面向对象(二)
  • flex加 grid 布局笔记
  • 最高评级!华为云CodeArts Board获信通院软件研发效能度量平台先进级认证
  • 图像上传功能实现
  • 03_Node.js模块化开发
  • Nginx支持SNI证书,已经ssl_server_name的使用
  • Hive【Hive(六)窗口函数】
  • Met no ‘TRANSLATIONS’ entry in project
  • Leetcode901-股票价格跨度
  • “传统文化宣传片+虚拟人动捕设备”前景如何?
  • 节假日moc服务数据:解决用户99%的IT问题
  • WOL唤醒配置(以太网、PHY、MAC)
  • MySQL复制,约束条件,查询与安全控制