跟着老侯玩编程 跟着老侯玩编程
首页
  • 基础语法
  • 网络编程
  • 设计模式
  • 基础篇
  • 进阶篇
  • 框架篇
  • Redis
  • Alibaba
  • 课程目录
  • 码农宝典
留言
首页
  • 基础语法
  • 网络编程
  • 设计模式
  • 基础篇
  • 进阶篇
  • 框架篇
  • Redis
  • Alibaba
  • 课程目录
  • 码农宝典
留言
  • 基础篇

    • 聊聊环境变量
    • 永不过时的HelloWorld
    • 标识符
    • 数据类型
    • 运算符
    • 语句
    • 数组
    • 方法详解
    • 类和对象
    • 类的成员
    • 类的继承
    • 类的形式
    • 封装和多态
    • 接口和Lambda表达式
    • 泛型编程
    • 常用API之函数式接口
    • 常用API
    • 异常机制
    • 多线程
    • 常用API之File类
    • IO流
    • 集合
    • 常用API之Stream流
    • 网络编程
    • 枚举
    • 注解和反射
  • 进阶篇

    • MySQL基础
    • 数据库-JDBC
    • HTML基础
    • CSS基础
    • JavaScript-基础
    • 服务器-Servlet
    • 服务器-Servlet3
    • 动态网页-JSP
    • Ajax
    • 前端项目工程化
    • ECMA2015
    • 模块化规范
  • 框架篇

    • 工具-Maven
    • 框架-MyBatis
    • 框架-Spring
    • 框架-SpringMVC
    • 工具-SpringBoot
    • 工具-Nginx
      • Nginx 是什么
      • 为什么选择 Nginx
      • 下载和安装
        • Linux 环境下安装
        • 目录结构说明
      • 常用配置
  • Java
  • 框架篇
舞动的代码
2022-08-30
目录

工具-Nginx

# Nginx 是什么

Apache、Lighttpd、Tomcat、Jetty、IIS,它们都是 Web 服务器

# 为什么选择 Nginx

更快

这表现在两个方面:一方面,在正常情况下,单次请求会得到更快的响应。另一方面,在高峰期(如有数以万计的并发请求),Nginx 可以比其他 Web 服务器更快地响应请求

高扩展性

Nginx 的设计极具扩展性,它完全是由多个不同功能、不同层次、不同类型且耦合度极低的模块组成

高可靠性

高可靠性是我们选择 Nginx 的最基本条件,因为 Nginx 的可靠性是大家有目共睹的,很多家高流量网站都在核心服务器上大规模使用 Nginx

低内存消耗

一般情况下,10000 个非活跃的 HTTP Keep-Alive 连接在 Nginx 中仅消耗 2.5MB 的内存,这是 Nginx 支持高并发连接的基础

高并发

Nginx 正常可以支持 5 万的并发,如果是集群模式下可以达到百万级别

Tomcat 默认配置的最大请求数是 150 个,即同时能支持 150 个并发

热部署

master 管理进程与 worker 工作进程的分离设计,使得 Nginx 能够提供热部署功能,即可以在 7×24 小时不间断服务的前提下,升级 Nginx 的可执行文件

# 下载和安装

官方地址:http://nginx.org/en/download.html

版本说明

  • Mainline version:主线版本,当前正在维护的稳定版

  • Stable version:标准版

  • Legacy versions:历史版本

注意:版本选择时选择偶数版本

# Linux 环境下安装

yum 源安装:yum install -y nginx

会附带一些乱七八糟的环境

通过源码编译安装(需要依赖 C 语言的编译环境)

yum -y install cpp binutils glibc glibc-kernheaders glibc-common glibc-devel gcc make gcc-c++ libstdc++-devel tcl

安装依赖

yum install zlib-devel pcre-devel -y

./configure 参数介绍

  • --prefix=PATH # 代表指定安装的路径

  • sbin-path=PATH # 安装完之后的二进制命令存放路径

  • modules-path=PATH # 各模块存放路径

  • conf-path=PATH # 默认配置文件路径

  • pid-path=PATH # pid 文件存放路径

  • --lock-path=PATH # 锁文件存放路径

  • --with-模块名 # 添加官方自带模块

  • --add-模块路径 # 添加第三方研发模块


./configure --prefix=/usr/local/nginx --sbin-path=/bin/ --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/opt/apps/nginx-1.22.0"
  nginx binary file: "/opt/apps/nginx-1.22.0/sbin"
  nginx modules path: "/opt/apps/nginx-1.22.0/modules"
  nginx configuration prefix: "/opt/apps/nginx-1.22.0/conf"
  nginx configuration file: "/opt/apps/nginx-1.22.0/conf/nginx.conf"
  nginx pid file: "/opt/apps/nginx-1.22.0/logs/nginx.pid"
  nginx error log file: "/opt/apps/nginx-1.22.0/logs/error.log"
  nginx http access log file: "/opt/apps/nginx-1.22.0/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

#编译安装
make install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

Linux 平台下载软件

  • curl -O http://nginx.org/download/nginx-1.22.0.tar.gz

  • wget http://nginx.org/download/nginx-1.22.0.tar.gz

Centos7 最小化安装默认情况下没有 wget(yum install -y wget)

# 目录结构说明

启动成功截图

start

Windows 平台下结束 Nginx 的进程:taskkill /IM nginx.exe /F

# 常用配置


#user  nobody;
worker_processes  1;

#nginx 的全局配置

#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 {
	#引入一个外部文件,引入 content-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;
	#TCP 连接的超时时间
    keepalive_timeout  65;
    upstream    hostss{
            #提供服务的列表
            server localhost:8081 weight=1;
            server localhost:8082 weight=10;
    }
    #gzip  on;
	#定义站点配置 http://localhost/api
    server {
		#当前站点监听的端口
        listen       80;
		#当前站点的 IP
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
		#匹配 url
        location / {
            proxy_pass   http://hostss;
			#指定代码路径
            root  D:\\xc-ui-pc-static-portal;
			#定义默认首页文件
            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;
    #    }
    #}

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
工具-SpringBoot

← 工具-SpringBoot

Theme by Vdoing | Copyright © 2013-2023 冀ICP备16006233号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×