2022-03-11 Nginx基本使用
安装
安装教程很多,如果这个不行就再试试其它的。
安装位置
NGINX 和 NGINX Plus 与其他服务相似,因为它们使用以特定格式编写的基于文本的配置文件。
默认情况下,该文件名为nginx.conf,对于 NGINX Plus,该文件位于 /etc/nginx目录中。
对于nginx Open source,位置取决于用于安装 NGINX 的软件包系统和操作系统。它通常是 /usr/local/nginx/conf、/etc/nginx或 /usr/local/etc/nginx之一.)
操作命令
# 快速关闭Nginx,可能不保存相关信息,并迅速终止web服务
nginx -s stop
# 平稳关闭Nginx,保存相关信息,有安排的结束web服务
nginx -s quit
# 因改变了Nginx相关配置,需要重新加载配置而重载
nginx -s reload
# 重新打开日志文件
nginx -s reopen
# 为 Nginx 指定一个配置文件,来代替缺省的
nginx -c filename
# 不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件
nginx -t
# 显示 nginx 的版本
nginx -v
# 显示 nginx 的版本,编译器版本和配置参数
nginx -V
# 格式换显示 nginx 配置参数
2>&1 nginx -V | xargs -n1
2>&1 nginx -V | xargs -n1 | grep lua
如果提示nginx指令找不到,就去安装目录下/sbin/目录,使用./nginx [option]进行操作。
配置文件
user nobody; # 'main'上下文中的指令(全局配置)
events {
# 连接处理配置
}
http {
# 特定于HTTP并影响所有虚拟服务器的配置
server {
# HTTP虚拟服务器1的配置
location /one {
# 处理以'/one'开头的uri的配置
}
location /two {
# 处理以'/two'开头的uri的配置
}
}
server {
# HTTP虚拟服务器2的配置
}
}
stream {
# 特定于TCP/UDP的配置,影响所有虚拟服务器
server {
# 配置TCP虚拟服务器1
}
}
可以将配置文件分为全局配置、events配置、http配置(包含http全局配置以及server配置)、stream配置(对于TCP/UDP、UNIX socket进行控制)
nginx 官方配置文件手册 官方文档特别全,有特殊需求可以查看,下面说一些常用的配置参数
全局块
配置文件位置,在安装目录/conf/nginx.conf
user nobody; #设置nginx服务的系统使用用户
worker_processes 2; #工作进程数 一般情况与CPU核数保持一致
error_log logs/error.log #nginx的错误日志
pid logs/nginx.pid #nginx启动时的pid
这里user指nginx的worker的用户,而nginx的master是使用root权限,因为监听小于1000的端口需要较高的权限。
events块
events {
worker_connections 2048; #每个进程允许最大连接数
use epoll; #nginx使用的内核模型
}
use有select、poll、epoll三个,一般选用epoll性能更高。
http块
http全局块
http {
include mime.types; #文件扩展名与文件类型映射表 存储在conf/目录下
default_type application_octet-stream; #默认文件类型,默认为text/plain
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
server {
...
}
}
server块
server {
#server全局块
listen 80; #监听端口
server_name localhost; #监听名称(地址),可以在前缀或后缀使用*进行匹配
# ------------------------------------------------------------------
#location块 匹配uri
location / {
root html; #根目录位置 html为nginx路径下html/目录
index index.html index.htm; #定义路径下默认访问的文件名,一般跟着root放
deny 127.0.0.2; #拒绝的地址
alllow 127.0.0.1; #允许的地址
}
# 静态文件存储
location /files/ { #[ = | ~ | ~* | ^~ ] uri { ... } 可以添加参数
root /data/test/;
autoindex on;
autoindex_exact_size on; # 默认为on,显示出文件的确切大小,单位是bytes。改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
autoindex_localtime on; #默认为off,显示的文件时间为GMT时间。改为on后,显示的文件时间为文件的服务器时间
}
}
# ------------------------------------------------------------------
#热备
upstream realserver2 {
# ip_hash; Nginx会让相同的客户端ip请求相同的服务器(负载均衡默认使用顺序轮询)
server 111.111.111.112:8080 weight=1 max_fails=1 fail_timeout=1;
server 111.111.111.113:8080 weight=2 max_fails=2 fail_timeout=2 backup; # backup表示备用,当111.111.111.112:8080挂掉之后再启用
#weight:加权(权重);max_fails:最大失败次数;fail_timeout:达到最大失败次数后,暂停服务x秒
}
#真实下游服务器
upstream realserver {
# ip_hash; Nginx会让相同的客户端ip请求相同的服务器(负载均衡默认使用顺序轮询)
server 111.111.111.112:8080 weight=1 max_fails=1 fail_timeout=1;
server 111.111.111.113:8080 weight=2 max_fails=2 fail_timeout=2;
}
#反向代理 server可以有多个
server {
listen 80;
server_name www.daili1.com;
#代理映射
localtion / {
proxy_pass http://realserver;
}
}
location块
location有多种匹配规则[ = | ~ | ~* | ^~ ] uri { … }
- '='精准匹配,需要uri完全一致
- '~'区分大小写
- '~*'不区分大小写
- '^~'前缀匹配
- 默认’‘+’^',并且优先级最低
优先级:= > ^~ > ~ | ~* > 最长前缀匹配 > /(‘~’ 和 '~*'优先级相同时,按照从上到下顺序)
基础使用(配置文件)
# 全局块
user root;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
# 事件块
events {
use epoll;
worker_connections 2048;
}
# http块
http {
# http全局块
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream testserver {
server localhost:8080;
}
# htpp server块
server {
listen 80;
server_name test;
#反向代理
location / {
proxy_pass http://testserver;
}
#静态资源
location ~* /files/ {
root /data/test/;
autoindex on; #开启文件列表索引查看
autoindex_exact_size on;
autoindex_localtime on;
}
#不区分大小写uri匹配
location ~* /order/ {
root /data/test2/;
}
#区分大小写uri匹配
location ~ /order/ {
root /data/test1/;
}
}
}
参考配置
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 81;
listen 82;
listen 83;
listen 84;
listen 85;
listen 86;
server_name _;
location / {
root /data/map/;
#add_header 'Access-Control-Allow-Origin' '*';
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
}
}
server {
listen 8081;
listen 8082;
listen [::]:8081;
server_name _;
location / {
root /home/html/xiangshan;
index index.html;
try_files $uri $uri/ /index.html;
}
# 象山wpfs api接口访问
location /api {
proxy_buffer_size 1024k;
proxy_buffers 16 1024k;
proxy_busy_buffers_size 2048k;
proxy_temp_file_write_size 2048k;
proxy_pass http://172.10.21.112:10019/api;
}
# 瓦片访问
location /map {
alias /data/wpfs_xiangshan/map;
}
location /data-export/ {
root /home/data/service/java/;
}
# error_page 404 /404.html;
# location = /404.html {
# }
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
}
server {
listen 80;
listen 8083;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root /home/html/pcview;
index index.html;
try_files $uri $uri/ /index.html;
}
# 宁波风光api接口访问
location /api {
proxy_buffer_size 1024k;
proxy_buffers 16 1024k;
proxy_busy_buffers_size 2048k;
proxy_temp_file_write_size 2048k;
proxy_pass http://172.10.21.112:18082/api;
#proxy_pass http://172.10.13.25:18082/api;
}
# 浙江范围nc文件静态资源访问
location /zhejiang {
root /data/minio_base;
autoindex on;
}
#色斑图静态资源访问
location /pngPath/ {
alias /data/nbfg/sftp/image/;
}
# 前端瓦片底图
location /map/ {
root /data/map/;
}
# 图片静态资源访问
location /data {
root /;
autoindex on;
}
location /mapPath/ {
proxy_pass http://127.0.0.1:81/;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
配置nginx服务器自启
cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
启动服务:
systemctl daemon-reload && systemctl enable nginx.service && systemctl start nginx.service && systemctl status nginx.service
参考文章
一文彻底读懂nginx中的location指令
解决Nginx出现403 forbidden (13: Permission denied)报错的四种方法