摘要
Nginx是一款轻量级的Web服务器、反向代理服务器。
正向代理与反向代理
* 正向代理隐藏真实客户端* 反向代理隐藏真实服务端
图片来自:反向代理为何叫反向
Nginx是一款轻量级的Web服务器、反向代理服务器。
正向代理与反向代理
正向代理隐藏真实客户端
反向代理隐藏真实服务端
正向代理:
反向代理:
图片来自:反向代理为何叫反向代理? – TommyyZ的回答
配置文件位置
通常根配置文件在/etc/nginx/nginx.conf
,里面引入了/etc/nginx/conf.d/*.conf
配置文件,以及/etc/nginx/sites-enabled/
目录的所有文件,sites-enabled
目录中通常存放软链接,指向/etc/nginx/sites-avaliable/
目录的文件。
重启服务应用配置
改完配置文件要重启Nginx服务,Ubuntu中命令如下。配置文件有问题会导致Nginx无法启动并报错。
sudo service nginx restart
配置文件格式
/etc/nginx/nginx.conf
文件示例如下。HTTP服务器的主要配置都在http { * }
中,http中用include
引入了conf.d
和sites-enabled
目录的配置。
注意,一行配置的末尾需要加分号。
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 80;
location / {
root /www/;
}
}
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
Server基本参数
http{}
中可包含多个server{}
,每个server是一个虚拟服务器(Virtual Server),服务器对应不同的域名 / 子域名 / 端口。
http {
server {
# 监听的端口。设置了default_server时,可以直接用IP地址访问,而不需要匹配域名。
listen 80 default_server;
# IPv6配置
listen [::]:80 default_server ipv6only=on;
# 绑定的域名,可以不指定,也可以通配符
server_name my-domain-1.com www.my-domain-1.com www.my-domain-2.com;
# ...
}
server {
# ...
}
}
域名重定向的配置
301跳转临时重定向
server {
listen 80;
server_name www.old-domain.com;
return 301 http://www.new-domain.com/$request_uri;
}
永久重定向
server {
server_name old-domain.com www.old-domain.com;
rewrite ^/(.*) http://www.new-domain.com/$1 permanent;
}
location
server中可以有多个location,表示不同的路径。
location支持的语法 location [=|~|~*|^~|@] pattern { ... }
=
,完全匹配~
,区分大小写的正则匹配~*
,不区分大小写的正则匹配^~
,前缀匹配- 没有修饰符:前缀匹配
多条 location 规则优先级:
- 精确匹配:
=
- 前缀匹配:
^~
(立刻停止后续的正则搜索) - 正则匹配:
~
或~*
(按文件中定义的顺序依次搜索) - 不带修饰符的前缀,且优先匹配更长更精确的,和定义的先后顺序无关
server {
listen 80;
# 这两个location都是前缀匹配,顺序不影响,优先匹配更长更精确的。
location / {
root /www/html/;
}
location /images/ {
alias /www/images/;
}
}
参考:
root与alias
可以在location中用root和alias配置静态文件服务器。
location / {
root /www/;
}
location / {
alias /www/;
}
两者对比见表。
– | root | alias |
---|---|---|
配置 location /loc/ | root /www/; | alias /www/; |
url path= /loc/test 时访问的文件 | /www/loc/test | /www/test |
解释 | root + path | root + (path - location) |
try_files
一个location中可以通过try_files指定依次尝试多个位置。
例如下面的写法,php文件没找到就会返回404页面。
location ~ \.php$ {
try_files $uri =404;
# ...
}
单页应用的配置(React、Vue)
单页应用和静态文件服务器类似,区别在于URL中有path但是没有匹配到文件时,继续尝试index.html文件,否则会产生404。
server {
location / {
root /www/front; # 前端文件路径
index index.html; # hash模式只配置访问html就可以了
try_files $uri $uri/ /index.html; # history模式下
}
}
path转发到端口
server {
# 把/api/v1/*的路径转发到本地监听8080端口的RestAPI服务器
location /api/v1/ {
proxy_pass http://localhost:8080/;
proxy_set_header Host $host;
proxy_buffering off;
proxy_pass_request_headers on;
}
}
PHP的配置
将xxx.php
请求转发到FastCGI服务器php7.2-fpm
模块。
fastcgi_pass
有两种配置方式,一种是指定sock文件,例如fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
;另一种是指定端口号,例如fastcgi_pass 127.0.0.1:9000;
fastcgi_param
指定了PHP脚本文件名,其中$document_root
表示root所在目录,即location或者server中指定的root。- php文件放在
/www/php/
目录下,访问www.my-domain.com/test.php
时执行的是/www/php/test.php
文件。
location ~ \.php$ {
root /www/php/;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page与错误页面指定
server {
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
expires与过期时间指定
location ~ .*\.(js|css)$ {
expires 1h;
}
location ~ .*\.(gif|ico|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
最后,欢迎扫码关注微信公众号。程序员同行学习交流,聊天交友,国内外名企求职内推(微软 / 小冰 / Amazon / Shopee / Coupang / ATM / 头条 / 拼多多等),可加我微信 jzj2015 进技术群(备注进技术群,并简单自我介绍)。

本文由jzj1993原创,转载请注明来源:https://www.paincker.com/nginx-config
(标注了原文链接的文章除外)
暂无评论