天天快资讯丨Nginx 常用的基础配置(web前端相关方面)

2023-02-11 15:09:16 | 来源:腾讯云


(资料图)

最近很多朋友通过趣站网问到Nginx配置前端 web 服务,所以特地写了这篇文章;希望能够帮助更多的朋友。

基础配置

user                            root;worker_processes                1;events {  worker_connections            10240;}http {  log_format                    "$remote_addr - $remote_user [$time_local] " ""$request" $status $body_bytes_sent " ""$http_referer" "$http_user_agent"";  include                       mime.types;  default_type                  application/octet-stream;  sendfile                      on;  #autoindex                    on;  #autoindex_exact_size         off;  autoindex_localtime           on;  keepalive_timeout             65;  gzip                          on;  gzip_disable                  "msie6";  gzip_min_length               100;  gzip_buffers                  4 16k;  gzip_comp_level               1;  gzip_types                  text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;  gzip_types                    "*";  gzip_vary                     off;  server_tokens                 off;  client_max_body_size          200m;  server {    listen                      80 default_server;    server_name                 _;    return                      403 /www/403/index.html;  }  include                       ../serve/*.conf;}

隐藏 Nginx 版本信息

http {  server_tokens         off;}

禁止ip直接访问80端口

server {  listen                80 default;  server_name           _;  return                500;}

启动 web 服务 (vue 项目为例)

server {  # 项目启动端口  listen            80;  # 域名(localhost)  server_name       _;  # 禁止 iframe 嵌套  add_header        X-Frame-Options SAMEORIGIN;    # 访问地址 根路径配置  location / {    # 项目目录    root     html;    # 默认读取文件    index           index.html;    # 配置 history 模式的刷新空白    try_files       $uri $uri/ /index.html;  }    # 后缀匹配,解决静态资源找不到问题  location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {     root           html/static/;  }    # 图片防盗链  location ~/static/.*\.(jpg|jpeg|png|gif|webp)$ {    root              html;    valid_referers    *.deeruby.com;    if ($invalid_referer) {      return          403;    }  }    # 访问限制  location /static {    root               html;    # allow 允许    allow              39.xxx.xxx.xxx;    # deny  拒绝    deny               all;  }}

PC端和移动端使用不同的项目文件映射

server {  ......  location / {    root /home/static/pc;    if ($http_user_agent ~* "(mobile|android|iphone|ipod|phone)") {      root /home/static/mobile;    }    index index.html;  }}

一个web服务,配置多个项目 (location 匹配路由区别)

server {  listen                80;  server_name           _;    # 主应用  location / {    root         html/main;    index               index.html;    try_files           $uri $uri/ /index.html;  }    # 子应用一  location ^~ /store/ {    proxy_pass          http://localhost:8001;    proxy_redirect      off;    proxy_set_header    Host $host;    proxy_set_header    X-Real-IP $remote_addr;    proxy_set_header    X-Forwarded-For    proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;  }    # 子应用二  location ^~ /school/ {    proxy_pass          http://localhost:8002;    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;  }    # 静态资源读取不到问题处理  rewrite ^/api/profile/(.*)$ /(替换成正确路径的文件的上一层目录)/$1 last;}# 子应用一服务server {  listen                8001;  server_name           _;  location / {    root         html/store;    index               index.html;    try_files           $uri $uri/ /index.html;  }    location ^~ /store/ {    alias               html/store/;    index               index.html index.htm;    try_files           $uri /store/index.html;  }    # 接口代理  location  /api {    proxy_pass          http://localhost:8089;  }}# 子应用二服务server {  listen                8002;  server_name           _;  location / {    root         html/school;    index               index.html;    try_files           $uri $uri/ /index.html;  }    location ^~ /school/ {    alias               html/school/;    index               index.html index.htm;    try_files           $uri /school/index.html;  }    # 接口代理  location  /api {    proxy_pass          http://localhost:10010;  }}

配置负载均衡

upstream my_upstream {  server                http://localhost:9001;  server                http://localhost:9002;  server                http://localhost:9003;}server {  listen                9000;  server_name           test.com;  location / {    proxy_pass          my_upstream;    proxy_set_header    Host $proxy_host;    proxy_set_header    X-Real-IP $remote_addr;    proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;  }}

SSL 配置 HTTPS

server {  listen                      80;  server_name                 www.xxx.com;  # 将 http 重定向转移到 https  return 301 https://$server_name$request_uri;}server {  listen                      443 ssl;  server_name                 www.xxx.com;  ssl_certificate             /etc/nginx/ssl/www.xxx.com.pem;  ssl_certificate_key         /etc/nginx/ssl/www.xxx.com.key;  ssl_session_timeout         10m;  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;    location / {    root                    /project/xxx;    index                   index.html index.htm index.md;    try_files               $uri $uri/ /index.html;  }}
上一篇 下一篇

相关新闻

天天快资讯丨Nginx 常用的基础配置(web前端相关方面)

环球速看:TS大名单曝光,暖阳依旧核心,冬冠赛制更改闹乌龙

反义寡核苷酸疗法穿过血脑屏障-即时看

老吾老以及人之老的意思_关于老吾老以及人之老的意思的介绍

coacht恤多少钱 coacht恤好穿吗

实时:新药组合延长了一些肺癌患者的生存期

千牛群发消息怎么发?千牛群艾特@功能介绍

女的丧事送仗如何写

世界资讯:软文操作如何达到读者、客户、媒体的“三赢”?

焦点速递!施特劳斯简介_施特劳斯简介

热讯:至今已有千年历史 南安丰州桃源古街环境“大变样”

环球即时:美亚利桑那州州长窜台 外交部回应

好日子送什么礼品

试驾型格HATCHBACK e:HEV,两厢+2.0L混动,实车表现如何?

安哲

最新新闻

天天快资讯丨Nginx 常用的基础配置(web前端相关方面)

环球速看:TS大名单曝光,暖阳依旧核心,冬冠赛制更改闹乌龙

反义寡核苷酸疗法穿过血脑屏障-即时看

老吾老以及人之老的意思_关于老吾老以及人之老的意思的介绍

coacht恤多少钱 coacht恤好穿吗

实时:新药组合延长了一些肺癌患者的生存期

千牛群发消息怎么发?千牛群艾特@功能介绍

女的丧事送仗如何写

世界资讯:软文操作如何达到读者、客户、媒体的“三赢”?

焦点速递!施特劳斯简介_施特劳斯简介

热讯:至今已有千年历史 南安丰州桃源古街环境“大变样”

环球即时:美亚利桑那州州长窜台 外交部回应

好日子送什么礼品

试驾型格HATCHBACK e:HEV,两厢+2.0L混动,实车表现如何?

安哲

索尼soundbar_索尼Soundbar音质效果怎么样求专业人士介绍一番-热头条

031期阿宝排列三预测奖号:组选分布推荐 环球热门

株洲荷塘区:居民代表“持证上岗” 构建服务群众新模式

破伤风针有多疼_破伤风针疼吗

全球短讯!鸭肉怎么做好吃

人造肉板块2月9日涨0.69%,山东赫达领涨,主力资金净流出3465.02万元

焦点短讯!小米红米手机官网_小米红米手机官网首页

东宫为什么下架又上架_东宫为什么下架 天天亮点

【环球热闻】99朵玫瑰代表什么,送99朵玫瑰代表什么?

药字组词的组词

十三星座分别是什么座

速看!金塔县公安局劝阻一起电信诈骗案件,止损58万元!-天天实时

天天报道:急企业之所急,宝山探索“先投后股”标准化工作体系推动成果转化

ipad清理缓存的方法_ipad清理缓存

巴西烤肉串的做法_巴西烤串家庭简化版做法分享