搜索
网站建设,网站优化,网络营销,app开发,小程序开发,全网营销

400-825-2717互联网开发&推广服务提供商

与我们合作

我们专注:网站策划设计、网络舆论监控、网站优化及网站营销、品牌策略与设计
主营业务:网站建设、移动端微信小程序开发、APP开发、网络运营、云产品·运维解决方案

有一个品牌项目想和我们谈谈吗?

您可以填写右边的表格,让我们了解您的项目需求,这是一个良好的开始,我们将会尽快与您取得联系。当然也欢迎您给我们写信或是打电话,让我们听到您的声音

您也可通过下列途径与我们取得联系:

地 址: 上海市长宁区华宁国际7L

电 话: 400-825-2717(咨询专线)

电 话: 13054973230(售后客户服务)

网 址: http://www.56gw.net

传 真: 021-61488448

邮 箱: admin@wumujituan.com

快速提交您的需求 ↓

Nginx常用配置

发布日期:2024-02-26 浏览次数:6140

一、基础配置

  1. user                            root;

  2. worker_processes                1;


  3. events {

  4.   worker_connections            10240;

  5. }


  6. http {

  7.   log_format                    '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"';

  8.   include                       mime.types;

  9.   default_type                  application/octet-stream;

  10.   sendfile                      on;

  11.   #autoindex                    on;

  12.   #autoindex_exact_size         off;

  13.   autoindex_localtime           on;

  14.   keepalive_timeout             65;

  15.   gzip                          on;

  16.   gzip_disable                  "msie6";

  17.   gzip_min_length               100;

  18.   gzip_buffers                  4 16k;

  19.   gzip_comp_level               1;

  20.   gzip_types                  text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

  21.   gzip_types                    "*";

  22.   gzip_vary                     off;

  23.   server_tokens                 off;

  24.   client_max_body_size          200m;


  25.   server {

  26.     listen                      80 default_server;

  27.     server_name                 _;

  28.     return                      403 /www/403/index.html;

  29.   }


  30.   include                       ../serve/*.conf;

  31. }

复制代码


二、隐藏 Nginx 版本信息

  1. http {

  2.   server_tokens         off;

  3. }

复制代码



三、禁止ip直接访问80端口

  1. server {

  2.   listen                80 default;

  3.   server_name           _;

  4.   return                500;

  5. }

复制代码


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

  1. server {

  2.   # 项目启动端口

  3.   listen            80;

  4.   # 域名(localhost)

  5.   server_name       _;

  6.   # 禁止 iframe 嵌套

  7.   add_header        X-Frame-Options SAMEORIGIN;

  8.   

  9.   # 访问地址 根路径配置

  10.   location / {

  11.     # 项目目录

  12.     root        html;

  13.     # 默认读取文件

  14.     index           index.html;

  15.     # 配置 history 模式的刷新空白

  16.     try_files       $uri $uri/ /index.html;

  17.   }

  18.   

  19.   # 后缀匹配,解决静态资源找不到问题

  20.   location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {

  21.     root           html/static/;

  22.   }

  23.   

  24.   # 图片防盗链

  25.   location ~/static/.*\.(jpg|jpeg|png|gif|webp)$ {

  26.     root              html;

  27.     valid_referers    *.deeruby.com;

  28.     if ($invalid_referer) {

  29.       return          403;

  30.     }

  31.   }

  32.   

  33.   # 访问限制

  34.   location /static {

  35.     root               html;

  36.     # allow 允许

  37.     allow              39.xxx.xxx.xxx;

  38.     # deny  拒绝

  39.     deny               all;

  40.   }

  41. }

复制代码


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

  1. server {

  2.   ......

  3.   location / {

  4.     root /home/static/pc;

  5.     if ($http_user_agent ~* '(mobile|android|iphone|ipad|phone)') {

  6.       root /home/static/mobile;

  7.     }

  8.     index index.html;

  9.   }

  10. }

复制代码


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

  1. server {

  2.   listen                80;

  3.   server_name           _;

  4.   

  5.   # 主应用

  6.   location / {

  7.     root                html/main;

  8.     index               index.html;

  9.     try_files           $uri $uri/ /index.html;

  10.   }

  11.   

  12.   # 子应用一

  13.   location ^~ /store/ {

  14.     proxy_pass          http://localhost:8001;

  15.     proxy_redirect      off;

  16.     proxy_set_header    Host $host;

  17.     proxy_set_header    X-Real-IP $remote_addr;

  18.     proxy_set_header    X-Forwarded-For

  19.     proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;

  20.   }

  21.   

  22.   # 子应用二

  23.   location ^~ /school/ {

  24.     proxy_pass          http://localhost:8002;

  25.     proxy_redirect      off;

  26.     proxy_set_header    Host $host;

  27.     proxy_set_header    X-Real-IP $remote_addr;

  28.     proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;

  29.   }

  30.   

  31.   # 静态资源读取不到问题处理

  32.   rewrite ^/api/profile/(.*)$ /(替换成正确路径的文件的上一层目录)/$1 last;

  33. }


  34. # 子应用一服务

  35. server {

  36.   listen                8001;

  37.   server_name           _;

  38.   location / {

  39.     root                html/store;

  40.     index               index.html;

  41.     try_files           $uri $uri/ /index.html;

  42.   }

  43.   

  44.   location ^~ /store/ {

  45.     alias               html/store/;

  46.     index               index.html index.htm;

  47.     try_files           $uri /store/index.html;

  48.   }

  49.   

  50.   # 接口代理

  51.   location  /api {

  52.     proxy_pass          http://localhost:8089;

  53.   }

  54. }


  55. # 子应用二服务

  56. server {

  57.   listen                8002;

  58.   server_name           _;

  59.   location / {

  60.     root                html/school;

  61.     index               index.html;

  62.     try_files           $uri $uri/ /index.html;

  63.   }

  64.   

  65.   location ^~ /school/ {

  66.     alias               html/school/;

  67.     index               index.html index.htm;

  68.     try_files           $uri /school/index.html;

  69.   }

  70.   

  71.   # 接口代理

  72.   location  /api {

  73.     proxy_pass          http://localhost:10010;

  74.   }

  75. }

复制代码


七、配置负载均衡

  1. upstream my_upstream {

  2.   server                http://localhost:9001;

  3.   server                http://localhost:9002;

  4.   server                http://localhost:9003;

  5. }


  6. server {

  7.   listen                9000;

  8.   server_name           test.com;


  9.   location / {

  10.     proxy_pass          my_upstream;

  11.     proxy_set_header    Host $proxy_host;

  12.     proxy_set_header    X-Real-IP $remote_addr;

  13.     proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;

  14.   }

  15. }

复制代码


八、SSL 配置 HTTPS

  1. server {

  2.   listen                      80;

  3.   server_name                 <a  target="_blank">www.xxx.com</a>;

  4.   # 将 http 重定向转移到 https

  5.   return 301 https://$server_name$request_uri;

  6. }


  7. server {

  8.   listen                      443 ssl;

  9.   server_name                 <a  target="_blank">www.xxx.com</a>;

  10.   ssl_certificate             /etc/nginx/ssl/www.xxx.com.pem;

  11.   ssl_certificate_key         /etc/nginx/ssl/www.xxx.com.key;

  12.   ssl_session_timeout         10m;

  13.   ssl_ciphers                 ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;

  14.   ssl_protocols               TLSv1 TLSv1.1 TLSv1.2;

  15.   ssl_prefer_server_ciphers   on;

  16.   

  17.   location / {

  18.     root                    /project/xxx;

  19.     index                   index.html index.htm index.md;

  20.     try_files               $uri $uri/ /index.html;

  21.   }

  22. }

复制代码



GO 知识
查看经典案例

TOP

QQ客服

免费电话

微信咨询 在线咨询 免费电话
获取报价
您的称呼:

*

您的电话:

*

您的邮箱:

*

提交 重置
重要的事情,电话里聊

接通客服

不方便的时候线上咨询,在线等哦