Article directory
- Preface
- 1. Download and install
- 2. Use
- 3. Commonly used configurations (continuously updated)
-
- 1. Set the port number and name
- 2. Set the path to the resource
- 3. Use try_files to solve the problem of unable to find files (404)
- 4. Interface proxy forwarding
- 5. Set the access path corresponding to the error code
- 6. Turn on compression mode
- 7. Start multiple services
- 8. Load balancing configuration
- 9. Upload files and upload time configuration
- Summarize
Preface
-
What is nginx?
Nginx is a high-performance http server/reverse proxy server and email (IMAP/POP3) proxy server.
Developed by Russian programmer Igor Sysoev, official testing nginx can support 50,000 concurrent connections,
and consumes very low resources such as CPU and memory. , the operation is very stable. -
Application scenarios
http server. Nginx is an http service that can provide http services independently. Can be used as a static web server.
Virtual host. Multiple websites can be virtualized on one server. For example, a virtual host used by a personal website.
Reverse proxy, load balancing. When the number of visits to the website reaches a certain level and a single server cannot satisfy user requests, multiple server clusters are needed and nginx can be used as a reverse proxy. And multiple servers can share the load evenly, and there will be no downtime due to a high load on a certain server and a certain server will not be idle.
1. Download and install
2. Use
windows use
Start nginx
Place the page resources directly in the html folder, then double-click nginx.exe or use the start nginx
command to open it
Close nginx
Execute the following command
Check the Nginx process command under Windows Task Manager:tasklist /fi "imagename eq nginx.exe"
Then executetaskkill /f /t /im nginx.exe
Exit the process
Or close it directly in the task manager (right click, select exit)
Check port number usage
tasklist | findstr "进程id号"
Script starts Nginx service
@echo off
rem 如果启动前已经启动nginx并记录下pid文件,会kill指定进程
nginx.exe -s stop
rem 测试配置文件语法正确性
nginx.exe -t -c conf/nginx.conf
rem 显示版本信息
nginx.exe -v
rem 按照指定配置去启动nginx
nginx.exe -c conf/nginx.conf
Mac use
- Install
brew install nginx
- Check nginx version
nginx -v
- View nginx installation information
brew info nginx
Open resource directory file
open /opt/homebrew/var/www
2Then update the corresponding file resource content
Start nginx
nginx
Visit localhos:8080
Close nginx
nginx -s stop
or
// 此命令在修改配置后执行不生效
sudo brew services stop nginx
Reload nginx
nginx -s reload
View Nginx configuration file
vim /opt/homebrew/etc/nginx/nginx.conf
You can alsoopen /opt/homebrew/etc/nginx/
open the file, you can also use other software to open the file
3. Commonly used configurations (continuously updated)
1. Set the port number and name
server {
# 设置端口号,一般设置四位数
listen 8000;
# 设置服务名称为 localhost 访问地址为 localhost:8000 或者是 本机IP:8000
# server_name localhost;
# 设置一些域名、别名等
server_name somename alias another.alias;
...
}
2. Set the path to the resource
The corresponding file path can be configured according to actual needs.
# 指向 html 目录
server {
...
location / {
root html;
...
}
}
# 指向 index/html 目录
server {
...
location / {
root index/html;
...
}
}
3. Use try_files to solve the problem of unable to find files (404)
server {
...
location / {
root /html
# try_files 将尝试你列出的文件并设置内部文件指向
# 即 try_files 依次检测 /index.html , /index.htm , /$uri 是否存在,若不存在则重定向到@router
try_files $uri $uri/ @router;
index index.html index.htm;
gzip_static on;
}
# 对应上面的 @router ,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
# 因此需要rewrite到index.html(即根目录html)中,然后交给路由在处理请求资源
location @router {
rewrite ^.*$ /html/index.html last;
}
}
4. Interface proxy forwarding
server {
...
location /api {
# 如果实际接口没有当前代理前缀,需要重写路径
# add_header backendIP $upstream_addr; # 设置响应头显示转发实际地址
# add_header backendCode $upstream_status; # 设置响应头显示转发实际状态码
# rewrite ^/api/?(.*)$ /$1 break;
# 可根据实际情况设置代理请求头
# 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_pass http://170.0.0.0:1757; # 转发地址
# proxy_redirect http://170.0.0.0:1757 ;# 代理重定向
}
}
5. Set the access path corresponding to the error code
server {
...
# 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;
}
}
6. Turn on compression mode
server {
...
location / {
# root html;
gzip_static on;
}
}
7. Start multiple services
Just copy multiple servers and configure different port numbers, etc.
server {
listen 8001;
server_name localhost;
...
location / {
...
}
}
server {
listen 8002;
server_name localhost;
...
location / {
...
}
}
8. Load balancing configuration
If a service is provided by multiple servers, the load needs to be distributed to different servers for processing, and load balancing is required. That is, the agent configures multiple servers
upstream tomcat {
# 可以根据服务器的实际情况调整服务器权重。权重越高分配的请求越多,权重越低,请求越少。默认是都是1
server 172.0.0.0:8001 weight=2;
server 172.0.0.0:8002;
server 172.0.0.0:8003;
}
server {
listen 8001;
server_name localhost;
...
location / {
proxy_pass http://tomcat; # 转发地址
...
}
}
9. Upload files and upload time configuration
Just set the following in the http location
client_max_body_size 50m; // 限制文件大小
client_header_timeout 5m; // 时间 5分钟
client_body_timeout 5m; 时间 5分钟
proxy_connect_timeout 600s; // 时间 600秒
proxy_read_timeout 10m; // 时间 10分钟
proxy_send_timeout 10m; // 时间 10分钟
keepalive_timeout 65;
Summarize
# nginx
官方下载地址:[url](http://nginx.org/en/download.html)
[参考](https://cloud.tencent.com/developer/article/1333800)
nginx代理
查看Nginx的版本号:`nginx -V`
启动Nginx:`start nginx`
快速停止或关闭Nginx:`nginx -s stop`
正常停止或关闭Nginx:`nginx -s quit`
配置文件修改重装载命令:`nginx -s reload`
查看端口号占用情况:`tasklist | findstr "进程id号"`
查看windows任务管理器下Nginx的进程命令:`tasklist /fi "imagename eq nginx.exe"`
然后执行 `taskkill /f /t /im nginx.exe`退出进程