Nginx 常用配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
# cache
# proxy_connect_timeout 5;
# proxy_read_timeout 60;
# proxy_send_timeout 5;
# proxy_buffer_size 16k;
# proxy_buffers 4 64k;
# proxy_busy_buffers_size 128k;
# proxy_temp_file_write_size 128k;
# proxy_temp_path /home/temp_dir;
# proxy_cache_path /home/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
# end
#gzip on;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 8; #压缩级别
gzip_buffers 16 8k;
#gzip_http_version 1.1;
gzip_min_length 100; #不压缩临界值
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
server {
listen 80;
server_name localhost;
#
#add_header Content-Security-Policy "*";
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#使用 alias 虚拟路径代理新页面路由
location /datav {
# proxy_pass http://appserver;
# proxy_redirect off;
# proxy_set_header Host $host;
# proxy_cache cache_one;
# proxy_cache_valid 200 302 1h;
# proxy_cache_valid 301 1d;
# proxy_cache_valid any 1m;
# expires 30d;
add_header Cache-Control "no-cache, no-store";
# 新页面或项目资源放到 root 资源目录下的相应目录(不然会找不到资源)
alias D:/workspace/datav/datav/src;
try_files $uri $uri/ /datav/index.html;
index index.html index.htm;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
server {
listen 443 ssl;
#server_name localhost;
server_name www.china-parking.cn;
# 这个是证书的crt文件所在目录
#ssl_certificate cert.pem;
ssl_certificate 'D://Program Files//nginx//readme//www.china-parking.cn_chain.crt';
# 这个是证书key文件所在目录
#ssl_certificate_key cert.key;
ssl_certificate_key 'D://Program Files//nginx//readme//www.china-parking.cn_key.key';
#ssl_session_cache shared:SSL:1m;
#ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_redirect off;
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://192.168.20.100:8080/;
# 配置跨域
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';
if ($request_method = 'OPTIONS') {
return 204;
}
#root html;
root D:/workspace/datav/datav/src/main;
index index.html index.htm;
}
}
}
nginx.conf分块理解
可以把nginx.conf分为三个部分:全局块、events块、http区域

全局区域:从配置文件开始到events块之间的内容,主要会设置一些影响nginx服务器整体运行的配置指令,主要包括配置运行nginx服务器的用户(组)、允许生成的worker process数,进程PID存放路径、日志存放路径和类型以及配置文件的引入等。必须上面的第一行配置
#允许的work process数
worker_processes 1;
# 引入其他配置文件,其他配置文件可以放在conf目录下的domains文件夹下(没有自己创建一个)
include domains/helloWord.conf
这是Nginx服务器并发处理服务的关键配置,worker_processes值越大,可以支持的并发处理也越多,但是会受硬件、软件的设备的制约。
events区域:events块涉及的指令主要影响Nginx服务器与用户的网络连接,常用的设置包括是否开启对多worker_process下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个work process可以同时支持的最大链接数等。
worker_connections 1024;
样例中就表示每个work process支持的最大连接数为1024,这部分的配置对Nginx的性能影响较大,在实际中应该灵活配置。
http区域块:这块是Nginx服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。这里http块包含:http全局块、server 块

http全局块:http全局块配置的指定包括文件引入、MIME-TYPE定义、日志自定义、连接超时事件、单链接请求数上限等。
server块:这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。每个http块可以包括多个server块,而每个server块就相当于一个虚拟主机。每个server块也分为server全局块以及可以同时包含多个location块。
全局server块:最常见的配置就是本虚拟机主机的监听配置和本虚拟机的名称或IP配置。
location块:一个server块可以配置多个location块。这块的主要作用就是基于nginx服务器接收到的请求字符串(例如:server_name/uri-string),对虚拟主机名称(也可以是IP别名)之外的字符串(例如前面的/uri-string)进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等共鞥,还有许多第三方模块的配置也可以在这里进行。
nginx多情况处理
反向代理:
前端的主流解决跨域的方式就是:
1、开发生产cors解决;
2、开发proxy,生产nginx解决
#接口端
location /police/ {
proxy_pass http://192.168.12.122:8852/police/;
proxy_redirect default;
proxy_http_version 1.1;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 90;
}
解读如下:就是如果遇到以/police请求开头的接口,就去访问这个IP地址的后端接口。
定义多个端口的反向代理,直接copy上面的,直接修改代理头和proxy_pass即可。
开启gzip
开启gzip对于前端来说还是比较重要的,浏览器加载的包大概能节约一半的空间,例如首页需要加载b.js为900kb,在开启gzip后,浏览器会去加载b.js经过gzip之后的文件b.js.gz,大概只有450kb,能很好的提升浏览速度。
#在http全局配置块中添加如下配置
gzip on; # 默认off,是否开启gzip
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# 上面两个开启基本就能跑起了,下面的愿意折腾就了解一下
gzip_static on;
gzip_proxied any;
gzip_vary on;
gzip_comp_level 6;
gzip_buffers 16 8k;
# gzip_min_length 1k;
gzip_http_version 1.1;
前端页面刷新产生404问题
location / {
try_files $uri $uri/ /index.html;
}
这段代码的作用是,当用户刷新页面时,Nginx会先检查当前URL是否存在,如果不存在,就会尝试访问index.html,从而可以正常显示页面。
维护页面
在系统维护升级时,希望用户可以打开我们的维护页面,配置如下:
# 系统临时维护请打开下面这行注释,并重启nginx,维护完毕后请注释下年这行,并重启nginx
# rewrite ^(.*)$ /maintainace.html break;
开启多个端口监听,维护多个上线网站
一个IP,利用nginx上线多个网站,例如访问192.168.1.154:8080访问我们的个人博客网站,访问192.168.1.154:8081访问我们的直播网站。
配置:直接复制server块即可,一个server块就代表了一个网站,需要修改下端口和文件的路径等内容。
# 第一个网站:个人博客项目配置
server {
listen 8080;
root /data/www/hexo;
index index.html;
location / {
try_files $uri $uri/ /index.html; # 路由模式history的修改
}
}
# 第二个网站:直播网站项目配置
server {
listen 8081;
root /data/www/geov;
index index.html;
location / {}
}
动态资源与静态资源分离
在Web开发中,通常来说,动态资源其实就是指那些后台资源,而静态资源就是指HTML,JavaScript,CSS,img等文件。
一般来说,都需要将动态资源和静态资源分开,将静态资源部署在Nginx上,当一个请求来的时候,如果是静态资源的请求,就直接到nginx配置的静态资源目录下面获取资源,如果是动态资源的请求,nginx利用反向代理的原理,把请求转发给后台应用去处理,从而实现动静分离。
在使用前后端分离之后,可以很大程度的提升静态资源的访问速度,同时在开过程中也可以让前后端开发并行可以有效的提高开发时间,也可以有些的减少联调时间 。
解析:
实际上我们在代理前端的html和接口时就已经做到了动静分离。我们打包后的dist包实际上就是纯静态的资源,所以直接访问nginx静态资源服务器,就不需要经过后台的tomcat。访问接口时,就需要nginx先去访问后台服务器,拿到结果再给到浏览器
如何配置(其实是优化,因为一般配置会有动静分离):
假设有比较大的图片等需要放置到服务器上,然后通过nginx来转发。或者是有一个纯前端的开源项目(如pdf.js),也需要放在服务器上。如果放在本地代码包,打包出来体积会很大
location /image/ {
root /var/filecenter/;
}
location /static/ {
root /var/filecenter/;
}
location /car/ {
root /var/filecenter/;
}
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|js|css)$ {
root /Users/dalaoyang/Downloads/static;
}
Nginx 中配置指令说明
try_files uri uri index.html的作用 和 $uri的含义
try_files uri uri/ /index.html; 这句话是Nginx服务器配置中的一条指令,用于设置处理请求的策略。
uri:这是Nginx内置的一个变量,代表当前请求的URI,不包括参数部分。例如,如果请求的URL是http://example.com/user?id=1,那么$uri的值就是/user。
$uri/:尝试将请求作为目录处理,如果这个目录存在,Nginx会试图返回该目录下的默认文件(通常是index.html或index.htm)。
/index.html:如果前面的uri和uri/都无法找到对应的文件或目录,那么就返回/index.html文件。
因此,try_files uri uri/ /index.html; 的含义是:首先尝试按照请求的URI去寻找对应的文件,如果找不到,再尝试将请求作为目录处理,如果还是找不到,最后就返回/index.html文件。
location @router { rewrite ^.*$ /index.html last; } 配置说明
server {
listen 80;
server_name yourdomain.com;
location / {
try_files $uri $uri/ @router;
}
location @router {
rewrite ^.*$ /index.html last;
配置说明: 主location块 (location /): 这是捕获所有请求的location。它使用try_files指令来尝试按顺序查找请求的文件或目录。如果找不到对应的文件或目录,请求将被转发到@router location。 @router location块 : 这是一个特殊的location块,用于内部重定向。当请求无法匹配到任何静态文件或目录时,它会被重定向到@router。rewrite ^.* /index.html last;这条指令会将所有请求重写为对/index.html的请求,并使用last标志结束当前的重写过程,这意味着Nginx会再次处理这个请求,这次是针对/index.html的请求。 为什么使用@router? 使用@router可以让你集中处理所有无法直接通过静态文件服务的路由。这对于单页面应用(SPA)尤其有用,因为SPA的所有路由最终都应该由同一个HTML文件(通常是index.html)处理,并通过前端路由库(如React Router、Vue Router等)来动态加载内容。 注意事项: 确保你的应用能够正确处理前端路由。例如,如果你的前端框架是基于History API的(如React Router的BrowserRouter),确保服务器配置正确地回退到 index.html,否则刷新页面时可能会遇到404错误。 使用 try_files uri $uri/ @router;而不是直接将所有请求都重写到index.html可以提供更好的性能,因为它首先尝试提供静态文件或目录,只有当这些都无法匹配时才进行重写。
location /file {
root /home/file;
index index.html index.htm;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080 ;
client_max_body_size 100m;
client_body_buffer_size 128k;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
proxy_connect_timeout 600; #nginx跟后端服务器连接超时时间(代理连接超时) proxy_read_timeout 600; #连接成功后,后端服务器响应时间(代理接收超时) proxy_send_timeout 600; #后端服务器数据回传时间(代理发送超时) proxy_buffer_size 32k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) proxy_temp_file_write_size 16k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
访问 index.html 正常,但访问图片 imgs/pic3.png 返回 403 Forbidden,这通常是由于 权限问题、路径配置错误或防盗链机制 导致的
检查文件权限 确保 Nginx 进程用户(通常是 nginx 或 www-data)有权限访问图片文件:
# 查看图片权限
ls -l /path/to/nginx/html/imgs/pic3.png
# 修改权限(确保 Nginx 可读)
chmod 644 /path/to/nginx/html/imgs/pic3.png
chown nginx:nginx /path/to/nginx/html/imgs/pic3.png
# 递归修改整个目录权限(可选)
chmod -R 755 /path/to/nginx/html/imgs/
chown -R nginx:nginx /path/to/nginx/html/imgs/
注意:/path/to/nginx/html 是您的 Nginx 根目录(如 /usr/share/nginx/html 或 /var/www/html)。如果 Nginx 用户是 www-data,请替换 nginx:nginx 为 www-data:www-data。
Nginx 配置 php
server
{
listen 8088;
server_name 192.168.56.3 gptbt.online;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/gptbt.online/public;
#CERT-APPLY-CHECK--START
# 用于SSL证书申请时的文件验证相关配置 -- 请勿删除
include /www/server/panel/vhost/nginx/well-known/gptbt.online.conf;
#CERT-APPLY-CHECK--END
#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
#SSL-END
#ERROR-PAGE-START 错误页配置,可以注释、删除或修改
#error_page 404 /404.html;
#error_page 502 /502.html;
#ERROR-PAGE-END
#PHP-INFO-START PHP引用配置,可以注释或修改
include enable-php-74.conf;
#PHP-INFO-END
#REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
include /www/server/panel/vhost/rewrite/gptbt.online.conf;
#REWRITE-END
#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
#一键申请SSL证书验证目录相关设置
location ~ \.well-known{
allow all;
}
#禁止在证书验证目录放入敏感文件
if ( $uri ~ "^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$" ) {
return 403;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log /dev/null;
access_log /dev/null;
}
location ~ .*\.(js|css)?$
{
expires 12h;
error_log /dev/null;
access_log /dev/null;
}
access_log /www/wwwlogs/gptbt.online.log;
error_log /www/wwwlogs/gptbt.online.error.log;
}

5952

被折叠的 条评论
为什么被折叠?



