一、Nginx配置介绍

Nginx是一个高性能的‌HTTP和反向代理服务器,以其轻量级、高并发能力而闻名。它的配置文件通常位于 /etc/nginx/nginx.conf,但具体位置可能会因安装方式和操作系统而异。Nginx的配置文件主要由以下几个部分组成:

  1. 全局块:配置影响Nginx全局的指令,如运行Nginx服务器的用户组、Nginx进程PID存放路径、日志存放路径、配置文件引入、允许生成的worker process数等。
  2. events块:配置影响Nginx服务器或与用户的网络连接,包括每个进程的最大连接数、选取哪种事件驱动模型处理连接请求、是否允许同时接受多个网络连接等。
  3. http块:可以嵌套多个server,配置代理、缓存、日志定义等绝大多数功能和第三方模块的配置,如文件引入、MIME类型定义、日志自定义、是否使用sendfile传输文件、连接超时时间等。
  4. server块:配置虚拟主机的相关参数,一个http中可以有多个server块,用于定义不同的虚拟主机或网站。
  5. location块:配置请求的路由,以及各种页面的处理情况,如静态文件、动态页面处理等。

‌Nginx相关地址

源码:trac.nginx.org/nginx/brows…

官网:www.nginx.org/

如果你下载好啦,你的安装文件,不妨打开conf文件夹的nginx.conf文件,Nginx服务器的基础配置,默认的配置也存放在此。

在 nginx.conf 的注释符号为: #

默认的 nginx 配置文件 nginx.conf 内容如下:

 1#user  nobody;
 2worker_processes  1;
 3
 4#error_log  logs/error.log;
 5#error_log  logs/error.log  notice;
 6#error_log  logs/error.log  info;
 7
 8#pid        logs/nginx.pid;
 9
10events {
11    worker_connections  1024;
12}
13
14http {
15    include       mime.types;
16    default_type  application/octet-stream;
17
18    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
19    #                  '$status $body_bytes_sent "$http_referer" '
20    #                  '"$http_user_agent" "$http_x_forwarded_for"';
21
22    #access_log  logs/access.log  main;
23
24    sendfile        on;
25    #tcp_nopush     on;
26
27    #keepalive_timeout  0;
28    keepalive_timeout  65;
29
30    #gzip  on;
31
32    server {
33        listen       80;
34        server_name  localhost;
35
36        #charset koi8-r;
37
38        #access_log  logs/host.access.log  main;
39
40        location / {
41            root   html;
42            index  index.html index.htm;
43        }
44
45        #error_page  404              /404.html;
46
47        # redirect server error pages to the static page /50x.html
48        #
49        error_page   500 502 503 504  /50x.html;
50        location = /50x.html {
51            root   html;
52        }
53
54        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
55        #
56        #location ~ \.php$ {
57        #    proxy_pass   http:
58        #}
59
60        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
61        #
62        #location ~ \.php$ {
63        #    root           html;
64        #    fastcgi_pass   127.0.0.1:9000;
65        #    fastcgi_index  index.php;
66        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
67        #    include        fastcgi_params;
68        #}
69
70        # deny access to .htaccess files, if Apache's document root
71        # concurs with nginx's one
72        #
73        #location ~ /\.ht {
74        #    deny  all;
75        #}
76    }
77
78
79    # another virtual host using mix of IP-, name-, and port-based configuration
80    #
81    #server {
82    #    listen       8000;
83    #    listen       somename:8080;
84    #    server_name  somename  alias  another.alias;
85
86    #    location / {
87    #        root   html;
88    #        index  index.html index.htm;
89    #    }
90    #}
91
92
93    # HTTPS server
94    #
95    #server {
96    #    listen       443 ssl;
97    #    server_name  localhost;
98
99    #    ssl_certificate      cert.pem;
100    #    ssl_certificate_key  cert.key;
101
102    #    ssl_session_cache    shared:SSL:1m;
103    #    ssl_session_timeout  5m;
104
105    #    ssl_ciphers  HIGH:!aNULL:!MD5;
106    #    ssl_prefer_server_ciphers  on;
107
108    #    location / {
109    #        root   html;
110    #        index  index.html index.htm;
111    #    }
112    #}
113
114}

nginx 文件结构

 1
 2...              #全局块
 3
 4events {         #events块
 5   ...
 6}
 7
 8http      #http块
 9{
10    ...   #http全局块
11    server        #server块
12    { 
13        ...       #server全局块
14        location [PATTERN]   #location块
15        {
16            ...
17        }
18        location [PATTERN] 
19        {
20            ...
21        }
22    }
23    server
24    {
25      ...
26    }
27    ...     #http全局块
28}

下面给大家上一个配置文件,作为理解。

 1########### 每个指令必须有分号结束。#################
 2#user administrator administrators;  #配置用户或者组默认为nobody nobody
 3#worker_processes 2;  #允许生成的进程数默认为1
 4#pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址
 5error_log log/error.log debug;  #制定日志路径级别这个设置可以放入全局块http块server块级别以此为debug|info|notice|warn|error|crit|alert|emerg
 6events {
 7    accept_mutex on;   #设置网路连接序列化防止惊群现象发生默认为on
 8    multi_accept on;  #设置一个进程是否同时接受多个网络连接默认为off
 9    #use epoll;      #事件驱动模型select|poll|kqueue|epoll|resig|/dev/poll|eventport
10    worker_connections  1024;    #最大连接数默认为512
11}
12http {
13    include       mime.types;   #文件扩展名与文件类型映射表
14    default_type  application/octet-stream; #默认文件类型默认为text/plain
15    #access_log off; #取消服务日志    
16    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
17    access_log log/access.log myFormat;  #combined为日志格式的默认值
18    sendfile on;   #允许sendfile方式传输文件默认为off可以在http块server块location块
19    sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值默认为0即不设上限
20    keepalive_timeout 65;  #连接超时时间默认为75s可以在httpserverlocation块
21
22    upstream mysvr {   
23      server 127.0.0.1:7878;
24      server 192.168.10.121:3333 backup;  #热备
25    }
26    error_page 404 https:
27    server {
28        keepalive_requests 120; #单连接请求上限次数
29        listen       4545;   #监听端口
30        server_name  127.0.0.1;   #监听地址       
31        location  ~*^.+$ {       #请求的url过滤正则匹配~为区分大小写~*为不区分大小写
32           #root path;  #根目录
33           #index vv.txt;  #设置默认页
34           proxy_pass  http:
35           deny 127.0.0.1;  #拒绝的ip
36           allow 172.18.5.54; #允许的ip           
37        } 
38    }
39}

二、Nginx部署前端项目

要在Nginx上部署前端项目,你需要将前端项目的静态文件复制到Nginx的网站目录下,并配置Nginx来服务这些静态文件。以下是一个基本的步骤和示例配置:

  1. 将前端项目构建生成的静态文件(通常是index.html, 一些.js和.css文件,以及图片等资源)复制到Nginx的网站目录下,例如/usr/share/nginx/html
  2. 编辑Nginx配置文件,通常是/etc/nginx/nginx.conf或者/etc/nginx/conf.d/default.conf,或者在/etc/nginx/sites-available/目录下创建一个新的配置文件,然后使用ln -s链接到/etc/nginx/sites-enabled/。
  3. 在Nginx配置文件中,设置一个server块来处理静态文件的请求。
 1
 2server {
 3    listen 80; # 或者其他你想要监听的端口
 4    server_name example.com; # 你的域名或公网IP
 5    location / {
 6        root /path/to/your/frontend/project; # 前端项目的绝对路径
 7        index index.html;
 8        try_files $uri $uri/ /index.html; # 用于支持前端路由的HTML5 History模式
 9    }
10    # 如果需要处理API或其他后端服务可以添加一个location块
11    # location /api {
12    #     proxy_pass http:
13    # }
14}

在这个配置中:

  • listen 指定了Nginx监听的端口。
  • server_name 是你的域名或IP地址。
  • location / 块定义了对于根URL的请求应如何处理。
  • root 指定了前端项目文件所在的文件系统路径。
  • index 指定了默认页面。
  • try_files 指令用于检查文件是否存在于文件系统中,如果不存在则回退到/index.html,这对于单页应用(SPA)的路由很重要,因为它可以确保前端的路由工作。

确保替换/path/to/your/frontend/project为你的实际前端项目路径,并且确保Nginx用户有权限访问这个目录。

完成配置后,你需要重启或重新加载Nginx使配置生效:

sudo nginx -s reload 或者 sudo systemctl reload nginx

确保你的前端项目构建输出(通常是dist或build文件夹)包含了所有必要的静态文件,并且这些文件的权限允许Nginx用户访问。

个人笔记记录 2021 ~ 2025