Nginx学习(个人笔记)
off999 2025-01-04 22:24 21 浏览 0 评论
一、Nginx安装
- 安装
安装地址:http://nginx.org/en/download.html
我选择的是windows安装,下载成功后解压并安装,可以看到以下文件:
注意:安装目录不要有中文,否则会报错。
- 启动
建议用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项目到开发环境
- npm run build 打包文件,默认输出为dist;
- 将dist文件夹复制到nginx下面的html目录里面(dist可重命名);
- 修改配置文件:
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;
}
}
- 重启nginx,在cmd输入nginx -s reload
相关推荐
- 让 Python 代码飙升330倍:从入门到精通的四种性能优化实践
-
花下猫语:性能优化是每个程序员的必修课,但你是否想过,除了更换算法,还有哪些“大招”?这篇文章堪称典范,它将一个普通的函数,通过四套组合拳,硬生生把性能提升了330倍!作者不仅展示了“术”,更传授...
- 7 段不到 50 行的 Python 脚本,解决 7 个真实麻烦:代码、场景与可复制
-
“本文整理自开发者AbdurRahman在Stackademic的真实记录,所有代码均经过最小化删减,确保在50行内即可运行。每段脚本都对应一个日常场景,拿来即用,无需额外依赖。一、在朋...
- Python3.14:终于摆脱了GIL的限制
-
前言Python中最遭人诟病的设计之一就是GIL。GIL(全局解释器锁)是CPython的一个互斥锁,确保任何时刻只有一个线程可以执行Python字节码,这样可以避免多个线程同时操作内部数据结...
- Python Web开发实战:3小时从零搭建个人博客
-
一、为什么选Python做Web开发?Python在Web领域的优势很突出:o开发快:Django、Flask这些框架把常用功能都封装好了,不用重复写代码,能快速把想法变成能用的产品o需求多:行业...
- 图解Python编程:从入门到精通系列教程(附全套速查表)
-
引言本系列教程展开讲解Python编程语言,Python是一门开源免费、通用型的脚本编程语言,它上手简单,功能强大,它也是互联网最热门的编程语言之一。Python生态丰富,库(模块)极其丰富,这使...
- Python 并发编程实战:从基础到实战应用
-
并发编程是提升Python程序效率的关键技能,尤其在处理多任务场景时作用显著。本文将系统介绍Python中主流的并发实现方式,帮助你根据场景选择最优方案。一、多线程编程(threading)核...
- 吴恩达亲自授课,适合初学者的Python编程课程上线
-
吴恩达教授开新课了,还是亲自授课!今天,人工智能著名学者、斯坦福大学教授吴恩达在社交平台X上发帖介绍了一门新课程——AIPythonforBeginners,旨在从头开始讲授Python...
- Python GUI 编程:tkinter 初学者入门指南——Ttk 小部件
-
在本文中,将介绍Tkinter.ttk主题小部件,是常规Tkinter小部件的升级版本。Tkinter有两种小部件:经典小部件、主题小部件。Tkinter于1991年推出了经典小部件,...
- Python turtle模块编程实践教程
-
一、模块概述与核心概念1.1turtle模块简介定义:turtle是Python标准库中的2D绘图模块,基于Logo语言的海龟绘图理念实现。核心原理:坐标系系统:原点(0,0)位于画布中心X轴:向右...
- Python 中的asyncio 编程入门示例-1
-
Python的asyncio库是用于编写并发代码的,它使用async/await语法。它为编写异步程序提供了基础,通过非阻塞调用高效处理I/O密集型操作,适用于涉及网络连接、文件I/O...
- 30天学会Python,开启编程新世界
-
在当今这个数字化无处不在的时代,Python凭借其精炼的语法架构、卓越的性能以及多元化的应用领域,稳坐编程语言排行榜的前列。无论是投身于数据分析、人工智能的探索,还是Web开发的构建,亦或是自动化办公...
- Python基础知识(IO编程)
-
1.文件读写读写文件是Python语言最常见的IO操作。通过数据盘读写文件的功能都是由操作系统提供的,读写文件就是请求操作系统打开一个文件对象(通常称为文件描述符),然后,通过操作系统提供的接口从这个...
- Python零基础到精通,这8个入门技巧让你少走弯路,7天速通编程!
-
Python学习就像玩积木,从最基础的块开始,一步步搭建出复杂的作品。我记得刚开始学Python时也是一头雾水,走了不少弯路。现在回头看,其实掌握几个核心概念,就能快速入门这门编程语言。来聊聊怎么用最...
- 一文带你了解Python Socket 编程
-
大家好,我是皮皮。前言Socket又称为套接字,它是所有网络通信的基础。网络通信其实就是进程间的通信,Socket主要是使用IP地址,协议,端口号来标识一个进程。端口号的范围为0~65535(用户端口...
- Python-面向对象编程入门
-
面向对象编程是一种非常流行的编程范式(programmingparadigm),所谓编程范式就是程序设计的方法论,简单的说就是程序员对程序的认知和理解以及他们编写代码的方式。类和对象面向对象编程:把...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (77)
- python封装 (57)
- python写入txt (66)
- python读取文件夹下所有文件 (59)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)