百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

Nginx学习(个人笔记)

off999 2025-01-04 22:24 30 浏览 0 评论

一、Nginx安装

  1. 安装

安装地址:http://nginx.org/en/download.html

我选择的是windows安装,下载成功后解压并安装,可以看到以下文件:
注意:安装目录不要有中文,否则会报错。

  1. 启动

建议用cmd打开,直接双击nginx.exe会出现一闪而过的画面。进入到安装目录下:

start nginx

这时候到浏览器访问localhost:80端口(或者直接访问localhost,http默认协议为http,默认端口为80),就可以看见以下页面:

二、Nginx常用命令

start nginx —— 启动
nginx -s stop —— 停止
nginx -s quit —— 安全退出
nginx -s reload —— 重新加载配置文件 (一般修改配置文件之后就要执行此命令)

三、nginx.conf配置文件

1.全局块:
主要设置一些影响nginx 服务器整体运行的配置指令。如:用户(组),生成worker process 数,日志存放路径,配置文件引入,pid文件路径
2.events 块
影响nginx 服务器与用户的网络连接。如:每个worker processs 的最大连接数,事件驱动模型

3.http块
nginx 配置中的重要部分,代理,缓存等功能都可以在此配置。如:文件引入,mime-type 定义,连接超时,单连接请求数上限

3.1 .server 块
与“虚拟主机”的概念有密切关系。每个server 块相当于一台虚拟主机,最常见的两个配置项是监听配置和虚拟主机的名称或IP配置

3.1.1 .location 块
在server 块中,可以有多个。地址定向,数据缓存,和应答控制等功能都是在这部分实现。

详细配置:

# =========================全局块============================
# user user [grop];        # 指定可以运行nginx 服务器的用户及组。只被设置的用户及组才有权限管理
# user nobody nobody;      # 所用用户可用,默认配置
user nginx nginx; 
# worker_processes number | auto;   # 控制并发处理,值越大,支持的并发数越多
# worker_processes 1;               # 默认值 ,最多产生1个worker_processes
worker_processes auto;
# pid file;                  # pid 存放路径.相对或绝对路径
# pid logs/nginx.pid;        # 默认在安装目录logs/nginx.pid
pid /var/run/nginx.pid;
# error_log file | stder [debug | info | notice | warn | error | crit | alert | emerg];  #错误日志存放路径
# error_log logs/error.log error;  # 可在 全局块,http 块 ,serveer 块,location 块中配置
error_log /data/wwwlogs/error_nginx.log crit;
# include file;      # 配置文件引入,可以是相对/绝对路径。指令可以放在配置文件的任意地方(任意块中)
worker_rlimit_nofile 51200;
# ===========================events 块===============================
events {
    #accept_mutex on|off;      # 网络连接序列化。解决“惊群”问题
    accept_mutex on;           # 默认为开启状态
    #multi_accept on|off;      # 是否允许worker process同时接收多个网络连接,
    multi_accept off;          # 默认为关闭
    #use method;               # 事件驱动模型选择,select 、 poll 、 kqueue 、 epoll 、 rtsig 、 /dev/poll 、 eventport
    use epoll;
    #worker_connections number; # worker process 的最大连接数。 
    worker_connections 512;     # 默认值为512。注意设置不能大于操作系统支持打开的最大文件句柄数量。
}
# ==========================http块============================
http {
    # 定义 mime-type (网络资源的媒体类型)。  指定能识别前端请求的资源类型 
    include mime.types;   # 引入外部文件 ,在外部文件中有定义types 块
    default_type application/octet-stream;   # 默认为 text/plain 。 http块、server块、location块中可配置
    # access_log path [format[buffer=size]];  # 定义服务日志,记录前端的请求日志
    # access_log logs/access.log combined;    # combined 是默认定义的日志格式字符串名称
    access_log off;                          # 关闭日志
    # log_format name stirng      # 定义了日志格式,以便access_log 使用
    # log_format  combined  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    # sendfile on|off;       # 配置允许 sendfile 方式传输文件
    sendfile off;           # 默认关闭,可在http块、server块、location块 中定义
    # sendfile_max_chunk size;    # 配置worker process 每次调用sendfile()传输的数据最最大值
    # sendfile_max_chunk 0;        # 默认为0 不限制。  可在http块、server块、location块 中定义
    sendfile_max_chunk 128k;
    # keepalive_timeout timeout [header_timeout];    # 配置连接超时时间, 可在http块、server块、location块 中定义
    # keepalive_timeout 75s;        # 默认为75秒,与用户建立会话连接后,nginx 服务器可以保持这些连接打开的时间。
    keepalive_timeout 120 100s;    # 在服务器端保持连接的时间设置为120s,发给用户端的应答报文头部中keep-alive 域的设置为100s
    # keepalive_requests number;    # 单连接请求数上限
    keepalive_requests 100;        # 默认为 100
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 1024m;
    tcp_nopush on;
    server_tokens off;
    tcp_nodelay on;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    #Gzip Compression
    gzip on;
    gzip_buffers 16 8k;
    gzip_comp_level 6;
    gzip_http_version 1.1;
    gzip_min_length 256;
    gzip_proxied any;
    gzip_vary on;
    gzip_types
        text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
        text/javascript application/javascript application/x-javascript
        text/x-json application/json application/x-web-app-manifest+json
        text/css text/plain text/x-component
        font/opentype application/x-font-ttf application/vnd.ms-fontobject
        image/x-icon;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    # If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
    open_file_cache max=1000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
######################## default ############################
#    server {
#    listen 8067;
#    server_name _;
#    access_log /data/wwwlogs/access_nginx.log combined;
#    root /data/wwwroot/default;
#    index index.html index.htm index.jsp;
#    location /nginx_status {
#        stub_status on;
#        access_log off;
#        allow 127.0.0.1;
#        deny all;
#        }
#    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
#        expires 30d;
#        access_log off;
#        }
#    location ~ .*\.(js|css)?$ {
#        expires 7d;
#        access_log off;
#        }
#    location ~ {
#        proxy_pass http://127.0.0.1:8080;
#        include proxy.conf;
#        }
#    }
#    server{
#     listen 8087;
#    server_name _;
#     root /home/wwwroot/default;
#     location / {
#      }
#    }
    upstream backend{
      server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=2;
      server 139.224.209.104:8080 backup;
    }
    server{
      listen 80;
      listen 443 ssl;
      server_name wmcenter.xy-asia.com;
      #ssl on;
      default_type 'text/html';
      charset utf-8;
      ssl_certificate   /usr/local/cert/1634560_wmcenter.xy-asia.com.pem;
      ssl_certificate_key  /usr/local/cert/1634560_wmcenter.xy-asia.com.key;
      ssl_session_timeout 5m;
      ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      ssl_prefer_server_ciphers on;
      root html;
      index index.html index.htm;
      location ~ .*\.(js|css)?$
      {
         expires 1h;
         proxy_pass   http://backend;
      }
      location / {
        proxy_pass  http://backend;
    add_header backendIP $upstream_addr;
    # proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
        # 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_set_header X-Forwarded-Proto https;
        # proxy_redirect http://http://127.0.0.1:8080 $scheme://127.0.0.1:8080;
        # port_in_redirect on;
        # proxy_redirect     off;
        include proxy.conf;
      }
      # error_log /home/wwwlogs/xywm.log;
    }
     # 缓存配置
    proxy_temp_file_write_size 264k;
    proxy_temp_path /data/wwwlogs/nginx_temp;
    proxy_cache_path /data/wwwlogs/nginx_cache levels=1:2 keys_zone=prc:200m inactive=5d max_size=400m;
    proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; 
########################## vhost #############################
    include vhost/*.conf;
}

四、部署vue项目到开发环境

  1. npm run build 打包文件,默认输出为dist;
  2. 将dist文件夹复制到nginx下面的html目录里面(dist可重命名);
  3. 修改配置文件:
server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html/dist; // vue打包的文件所在的目录
            index  index.html index.htm;
            // try_files防止404,$uri表示当前路径,从前往后匹配,一直到/index.html
            try_files $uri $uri/ /index.html; 
        }
}
  1. 重启nginx,在cmd输入nginx -s reload

相关推荐

显卡排行天梯图2025(显卡排行天梯图mx350)

撼迅显卡来自我国台湾的厂商,在大陆的品牌叫做“迪兰恒进(Dataland)”,撼讯显卡做工精良,高端尤其出众。2004年撼讯出货量为400万片,位列全球第三,同时还是ATI的三大合作伙伴之一。对于喜欢...

主机无限重启一亮一灭(主机开机无限重启怎么回事)

分析:最常见故障为内存接触不良。处理:关机(如果短按电源开关无效则长按开关直到电源指示灯熄灭或直接拔掉电源线)断电开机箱,重新插拔内存条,并用橡皮顺着一个方向擦拭金手指(大拇指手指甲背刮也行),如有条...

经典word2003官方版(word经典版本是几几年的)

MicrosoftOffice2003有六个版本。分别是:入门版、学生教师版、标准版、小型企业版、专业版、企业用专业版。没叫“word97-2003”的,你讲的应该是保存类型(格式)吧?Word...

u盘不能格式化了咋办(u盘不能格式化是怎么回事)

答:u盘无法格式化最有效的方法步骤如下。点击系统与安全,进入电脑的控制面板界面,点击上方的系统与安全的选项,在系统界面找到最下方的管理工具功能组。选中u盘,选择管理工具下面的创建并格式化硬盘分区,点击...

win10进系统后黑屏只有鼠标(win10开机进系统黑屏只有鼠标)

explorer.exe-应用程序错误应用程序无法正常启动(0x0000018)。请单击“确定”关闭应用程序。问题原因不详,但是下面的操作方法解决了他的问题,也希望能给您提供一些切实的帮助!第一步、按...

惠普打印机下载什么驱动(惠普打印机用哪个驱动器)

HP惠普打印机驱动官网是存在的。因为HP惠普是一家著名的电脑及相关产品制造商,他们会提供各种驱动程序和软件以保证设备的兼容性和稳定性。HP惠普打印机驱动官网可以通过搜索引擎查找,也可以通过HP惠普的官...

xp系统安装office2010(XP系统安装哪个版本的输入法)

1、下载小白三步装机版软件,安装完成后进入页面中选择xp系统,点击立即重装按钮。?2、接着软件会自动下载WindowsXP镜像以及Pe系统。当系统安装部署完成后,择立即重启选项。?3、重启电脑...

电子邮箱地址是什么(遂宁的电子邮箱地址是什么)
电子邮箱地址是什么(遂宁的电子邮箱地址是什么)

邮箱被赋予的一个唯一的序号,就像我们居住的地址,用意确定收件人。Internet中每个用户的电子邮箱地址都具有惟一性,这样可使邮件的收发更加方便、准确。通常电子邮件地址的格式为:user@mail.server.name,其中user...

2025-12-10 23:03 off999

win10下载到u盘要多久(win10下到u盘多少g)

Win10下载到U盘所需的时间取决于多种因素,包括您的网络速度、计算机性能和U盘的读写速度。如果您的网络速度较快,下载Windows10的时间应该不会太长。从下载到将Windows10安装到U盘上...

cf烟雾头怎么调win10系统(cf里w10系统烟雾头怎么调)
  • cf烟雾头怎么调win10系统(cf里w10系统烟雾头怎么调)
  • cf烟雾头怎么调win10系统(cf里w10系统烟雾头怎么调)
  • cf烟雾头怎么调win10系统(cf里w10系统烟雾头怎么调)
  • cf烟雾头怎么调win10系统(cf里w10系统烟雾头怎么调)
c盘100g剩多少才合适(c盘100g还剩30g够用吗)

够用。但前提是我们只能把C盘当做系统盘,一般系统(win10)只占30到个40g,加上一些系统运行缓存文件,C盘剩下的空间大概在40到50个g。所以说你的其他软件千万不能安装在C盘了,安装在D盘或者F...

腾讯电脑管家官网网址(腾讯电脑管家介绍)

就是正常的输入你路由器的账户密码,如果你的默认密码没有修改。那你可以看看你路由器底下,底下一般会有路由器的初始账号密码。首先需要使用数据线链接,手机完成匹配之后,使用WIFI链接,会随机刷出验证码。一...

免费下载qq2025新版本手机(下载qq2020最新版)

1/4找到QQ注册的官网进入官网2/4填写信息申请页面中需要填写昵称,密码,验证码及其他信息,然后点击确定即可。3/4登录QQ完成上一步之后,页面上会显示申请的QQ号码,和登录QQ按钮,点击登录QQ。...

wifi满格无法上网(wifi满格无法上网怎么做)
  • wifi满格无法上网(wifi满格无法上网怎么做)
  • wifi满格无法上网(wifi满格无法上网怎么做)
  • wifi满格无法上网(wifi满格无法上网怎么做)
  • wifi满格无法上网(wifi满格无法上网怎么做)
win10模拟器电脑版下载(win10模拟器正版)

在Windows10模拟器上下载应用程序,可以按照以下步骤进行操作:1.打开Windows10模拟器:在电脑上打开Windows10模拟器软件,如MicrosoftVisualStudio...

取消回复欢迎 发表评论: