黄色三级免费网站_中文字幕久久伊人_狠狠影院_久久久国产一区_中文字幕欧美日韩_亚洲久久

Nginx 常用的基礎配置(web前端相關方面)

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


(相關資料圖)

最近很多朋友通過趣站網問到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

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

熱門資訊

主站蜘蛛池模板: 欧美日韩亚洲另类 | 国产精品欧美日韩一区二区 | 日本怡春院一区二区三区 | 日韩欧美区 | 曰本女人性配视频 | 亚洲国产天堂久久精品网 | 丰满岳乱妇一区二区三区 | 综合久久久久久久综合网 | 日本一级毛片在线播放 | 日本黄大片免播放视播放器 | 国产精品成人无码久久久 | 亚洲女人被黑人巨大进入 | 强奷乱码中文字幕 | 久久久影院 | 在线观看毛片网站 | 欧美一区二区三区日韩免费播 | 亚洲国产毛片aaaaa无费看 | 三级黄色片a | 国产一浮力影院 | 麻豆国产一区二区在线观看 | 日本精品视频 | 国产手机在线αⅴ片无码观看 | 九九99在线视频 | 888午夜不卡理论久久 | 国产精品亚洲一区二区三区正片 | av无码久久久久不卡免费网站 | 国产亚洲精品久久久久久久网站 | 亚洲高清一区二区三区电影 | 毛片网站免费 | 日本爱爱视频 | 99久99久6久热在线播放 | 黑人粗大猛烈进出高潮视频 | 91香蕉国产线在线观看免费 | 亚洲一级爽片 | 在线成年人网站 | 久久久国产精品免费 | 亚洲色图综合图区 | 国产六月婷婷爱在线观看 | 欧美一区二区三区网站 | 少妇人妻好深太紧了 | 色性综合 |