Nginx 常用的基礎(chǔ)配置(web前端相關(guān)方面)

                • 騰訊云
                • 2023-02-13 09:30:04


                (相關(guān)資料圖)

                最近很多朋友通過趣站網(wǎng)問到Nginx配置前端 web 服務(wù),所以特地寫了這篇文章;希望能夠幫助更多的朋友。

                基礎(chǔ)配置

                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;}

                啟動(dòng) web 服務(wù) (vue 項(xiàng)目為例)

                server {  # 項(xiàng)目啟動(dòng)端口  listen            80;  # 域名(localhost)  server_name       _;  # 禁止 iframe 嵌套  add_header        X-Frame-Options SAMEORIGIN;    # 訪問地址 根路徑配置  location / {    # 項(xiàng)目目錄    root     html;    # 默認(rèn)讀取文件    index           index.html;    # 配置 history 模式的刷新空白    try_files       $uri $uri/ /index.html;  }    # 后綴匹配,解決靜態(tài)資源找不到問題  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端和移動(dòng)端使用不同的項(xiàng)目文件映射

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

                一個(gè)web服務(wù),配置多個(gè)項(xiàng)目 (location 匹配路由區(qū)別)

                server {  listen                80;  server_name           _;    # 主應(yīng)用  location / {    root         html/main;    index               index.html;    try_files           $uri $uri/ /index.html;  }    # 子應(yīng)用一  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;  }    # 子應(yīng)用二  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;  }    # 靜態(tài)資源讀取不到問題處理  rewrite ^/api/profile/(.*)$ /(替換成正確路徑的文件的上一層目錄)/$1 last;}# 子應(yīng)用一服務(wù)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;  }}# 子應(yīng)用二服務(wù)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;  }}

                配置負(fù)載均衡

                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 重定向轉(zhuǎn)移到 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;  }}

                關(guān)鍵詞: Nginx

                分享到:
                ?
                • 至少輸入5個(gè)字符
                • 表情

                熱門資訊

                亚洲一级特黄特黄的大片| 亚洲gv白嫩小受在线观看| 亚洲国产第一页www| 久久亚洲色一区二区三区| 日韩一卡2卡3卡4卡新区亚洲 | 国产精品亚洲精品观看不卡| 亚洲黄色免费网站| 久久亚洲精品国产精品| 久久国产精品亚洲一区二区| 亚洲成AV人片在线观看无| 亚洲人成在线播放网站| 黑人精品videos亚洲人| 亚洲精品无码成人AAA片| 亚洲最大激情中文字幕| 国产亚洲精品成人AA片新蒲金| 国产午夜亚洲不卡| 国产亚洲人成无码网在线观看| 亚洲国产综合无码一区| 亚洲va久久久噜噜噜久久天堂| 久久久久亚洲AV成人无码网站| 亚洲国产美国国产综合一区二区| 久久亚洲一区二区| 久久精品亚洲一区二区三区浴池| 亚洲无删减国产精品一区| 久久精品亚洲一区二区三区浴池 | 国产精品日本亚洲777| 美国毛片亚洲社区在线观看| 亚洲 综合 国产 欧洲 丝袜| 亚洲日韩人妻第一页| 国产精品亚洲美女久久久| 亚洲综合无码AV一区二区| 亚洲妇熟XXXX妇色黄| 亚洲午夜精品一区二区| 亚洲嫩草影院在线观看| 99亚偷拍自图区亚洲| 日韩色日韩视频亚洲网站| 亚洲精品国产va在线观看蜜芽| 狠狠亚洲婷婷综合色香五月排名| 亚洲成A人片777777| 亚洲精选在线观看| 亚洲乱码一二三四五六区|