0
点赞
收藏
分享

微信扫一扫

小鹏汽车Q1财报:押注G6、大力降本,明年智驾BOM降半

洒在心头的阳光 2023-06-04 阅读 67

1 什么是Nginx

Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的。从2004年发布至今,凭借开源的力量,已经接近成熟与完善。

Nginx功能丰富,可作为HTTP服务器,也可作为反向代理服务器,邮件服务器。支持FastCGI、SSL、Virtual Host、URL Rewrite、Gzip等功能。并且支持很多第三方的模块扩展。

其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。在全球活跃的网站中有12.18%的使用比率,大约为2220万个网站。

Nginx 是一个安装非常的简单、配置文件非常简洁(还能够支持perl语法)、Bug非常少的服务。Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够不间断服务的情况下进行软件版本的升级。

Nginx代码完全用C语言从头写成。官方数据测试表明能够支持高达 50,000 个并发连接数的响应。

2 常见的代理模式

正向代理和反向代理:

 

前面是正向代理,后面是反向代理。

正向VPN: 虚拟专用网

 VPN英文全称:Virtual Private Network(虚拟专用网络)。 翻墙。

正向代理客户端,反向代理服务器。

3 负载均衡

Nginx提供的负载均衡策略有2种:内置策略和扩展策略。

内置策略为轮询,加权轮询,Ip hash。扩展策略,就天马行空,只有你想不到的没有他做不到的啦,你可以参照所有的负载均衡算法,给他一一找出来做下实现。

Ip hash算法,对客户端请求的ip进行hash操作,然后根据hash结果将同一个客户端ip的请求分发给同一台服务器进行处理,可以解决session不共享的问题。

4 Nginx安装

http://nginx.org/en/download.html下载稳定版本

下载------>解压------>启动。

 

 

 

5 Nginx的核心配置文件 nginx.conf

1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。

2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。

3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。

4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。

5、location块:配置请求的路由,以及各种页面的处理情况。

引用:Nginx 配置详解 | 菜鸟教程

 6 配置文件的常用配置:

引用:https://www.cnblogs.com/fanzhidongyzby/p/5194895.html

1.#Nginx所用用户和组,window下不指定  
2.user  nobody;  
3.  
4.#工作的子进程数量(通常等于CPU数量或者2倍于CPU)  
5.worker_processes  1;  
6.  
7.#错误日志存放路径  
8.#error_log  logs/error.log;  
9.#error_log  logs/error.log  notice;  
10.error_log  logs/error.log  info;  
11.  
12.#指定pid存放文件  
13.pid        logs/nginx.pid;  
14.  
15.  
16.events {  
17.#使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。  
18.use epoll;  
19.  
20.#允许最大连接数    
21.worker_connections  1024;  
22.}  
23.  
24.  
25.http {  
26.    #mine.types内定义各文件类型映像  
27.    include       mime.types;  
28.  
29.    default_type  application/octet-stream;  
30.      
31.    #定义日志格式  
32.    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
33.    #                  '$status $body_bytes_sent "$http_referer" '  
34.    #                  '"$http_user_agent" "$http_x_forwarded_for"';  
35.  
36.    #access_log  logs/access.log  main;  
37.  
38.    sendfile        on;  
39.    #tcp_nopush     on;  
40.  
41.    #keepalive_timeout  0;  
42.    keepalive_timeout  65;  
43.  
44.    #gzip  on;  
45.  
46.    #负载均衡集群设置  
47.    upstream tomcats {  
48.        server localhost:8080 weight=1;  
49.        server localhost:9080 weight=1;  
50.          
51.        #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。  
52.        #同一机器在多网情况下,路由切换,ip可能不同 ---测试的时候可以禁止此项,轮转更明显 
53.        ip_hash;  
54.    }     
55.  
56.    server {  
57.        listen       80;  
58.        server_name  localhost;  
59.  
60.        #charset koi8-r;  
61.  
62.        #access_log  logs/host.access.log  main;  
63.  
64.        location / {  
65.            index  index.shtml;  
66.            proxy_pass  http://tomcats;  
67.            proxy_set_header    X-Real-IP   $remote_addr;  
68.            client_max_body_size    100m;  
69.        }  
70.  
71.#配置动静态分离
72.#location ~ \.(jsp)?$ {
73.         #proxy_pass http://tomcats;
         #proxy_set_header Host $host;
         #proxy_set_header X-Real-IP $remote_addr;
         #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         #proxy_connect_timeout       100;
         #proxy_read_timeout          100;
         #proxy_send_timeout          100;
 #}
 #location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
         #  expires 30d;
         #  root  /usr/local/nginx/html;
         #}
74.
75.        #error_page  404              /404.html;  
76.  
77.        # redirect server error pages to the static page /50x.html  
78.        #  
79.        error_page   500 502 503 504  /50x.html;  
80.        location = /50x.html {  
81.            root   html;  
82.        }  
83.  
84.        # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
85.        #  
86.        #location ~ \.php$ {  
87.        #    proxy_pass   http://127.0.0.1;  
88.        #}  
89.  
90.        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
91.        #  
92.        #location ~ \.php$ {  
93.        #    root           html;  
94.        #    fastcgi_pass   127.0.0.1:9000;  
95.        #    fastcgi_index  index.php;  
96.        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
97.        #    include        fastcgi_params;  
98.        #}  
99.  
100.        # deny access to .htaccess files, if Apache's document root  
101.        # concurs with nginx's one  
102.        #  
103.        #location ~ /\.ht {  
104.        #    deny  all;  
105.        #}  
106.    }  
107.  
108.  
109.    # another virtual host using mix of IP-, name-, and port-based configuration  
110.    #  
111.    #server {  
112.    #    listen       8000;  
113.    #    listen       somename:8080;  
114.    #    server_name  somename  alias  another.alias;  
115.  
116.    #    location / {  
117.    #        root   html;  
118.    #        index  index.html index.htm;  
119.    #    }  
120.    #}  
121.  
122.  
123.    # HTTPS server  
124.    #  
125.    #server {  
126.    #    listen       443;  
127.    #    server_name  localhost;  
128.  
129.    #    ssl                  on;  
130.    #    ssl_certificate      cert.pem;  
131.    #    ssl_certificate_key  cert.key;  
132.  
133.    #    ssl_session_timeout  5m;  
134.  
135.    #    ssl_protocols  SSLv2 SSLv3 TLSv1;  
136.    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;  
137.    #    ssl_prefer_server_ciphers   on;  
138.    #    location / {  
139.    #        root   html;  
140.    #        index  index.html index.htm;  
141.    #    }  
142.    #}  
143.  
144.}  

引用:(238条消息) linux+nginx+tomcat集群-简单配置文件_deng-bb的博客-CSDN博客

linux+nginx+tomcat集群-简单配置文件

测试:

nginx+Tomcat的负载均衡配置_nginx配tomcat负载_采蘑菇的院长的博客-CSDN博客

上面链接能用

@RestController

@Slf4j

public class HelloController {

    @GetMapping("/hello")

    public String hello(){

        log.debug("我是"+ ServiceConfig.serverPort);

        return "好好学习"+ServiceConfig.serverPort;

    }

}



@Configuration

public class ServiceConfig implements ApplicationListener<WebServerInitializedEvent> {

    @Override

    public void onApplicationEvent(WebServerInitializedEvent event) {

        // 项目启动获取启动的端口号

        ServiceConfig.serverPort = event.getWebServer().getPort();

    }

    /**

     * 当前服务使用的端口号

     */

    public static int serverPort;

}

引用:

https://www.cnblogs.com/jiangwangxiang/p/8481661.html

启动2个Tomcat

https://blog.csdn.net/rosejeck/article/details/86149981

 

举报

相关推荐

0 条评论