前後端完全分離nginx配置參考

  1. admin.example.com.conf
server
    {
        listen 443 ssl http2;
        #listen [::]:443 ssl http2;
        server_name admin.example.com ;
        index index.html;
        root  /mnt/wwwroot/admin.example.com;

        ssl_certificate /usr/local/nginx/conf/ssl/admin.example.com/fullchain.cer;
        ssl_certificate_key /usr/local/nginx/conf/ssl/admin.example.com/admin.example.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
        ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
        ssl_session_cache builtin:1000 shared:SSL:10m;
        # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
        ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
         
        gzip_static on;
        
        location / {
          try_files $uri $uri/ /index.html last;
        }
        
        include rewrite/none.conf;
        #error_page   404   /404.html;

        location ~ .*\.(php|php5)$ {
          deny all; 
        }
        
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log off;
        error_log /home/wwwlogs/admin.example.com_error.log;
    }
  1. api.example.com.conf
server
    {
        listen 80;
        #listen [::]:80;
        server_name api.example.com ;
        index index.php;
        root  /mnt/wwwroot/api.example.com/public;

        include rewrite/thinkphp.conf;
        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include enable-php-pathinfo.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log off;
        error_log /home/wwwlogs/api.example.com_error.log;
    }

server
    {
        listen 443 ssl http2;
        #listen [::]:443 ssl http2;
        server_name api.example.com ;
        index index.php;
        root  /mnt/wwwroot/api.example.com/public;

        ssl_certificate /usr/local/nginx/conf/ssl/api.example.com/fullchain.cer;
        ssl_certificate_key /usr/local/nginx/conf/ssl/api.example.com/api.example.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
        ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
        ssl_session_cache builtin:1000 shared:SSL:10m;
        # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
        ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;

        include rewrite/thinkphp.conf;
        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include enable-php-pathinfo.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log off;
        error_log /home/wwwlogs/api.example.com_error.log;
    }
  1. static.example.com.conf
server
    {
        listen 80;
        #listen [::]:80;
        server_name static.example.com ;
        index index.html;
        root  /mnt/wwwroot/api.example.com/public;

        include rewrite/none.conf;
        #error_page   404   /404.html;
        
        location / {
          #   指定允許跨域的方法,*代表所有
          add_header Access-Control-Allow-Methods *;
  
          #   預檢命令的緩存,如果不緩存每次會發送兩次請求
          add_header Access-Control-Max-Age 3600;
          #   帶cookie請求需要加上這個字段,並設置爲true
          add_header Access-Control-Allow-Credentials true;
  
          #   表示允許這個域跨域調用(客戶端發送請求的域名和端口) 
          #   $http_origin動態獲取請求客戶端請求的域   不用*的原因是帶cookie的請求不支持*號
          add_header Access-Control-Allow-Origin $http_origin;
  
          #   表示請求頭的字段 動態獲取
          add_header Access-Control-Allow-Headers 
          $http_access_control_request_headers;
  
          #   OPTIONS預檢命令,預檢命令通過時才發送請求
          #   檢查請求的類型是不是預檢命令
          if ($request_method = OPTIONS){
              return 200;
          }
        }
        
        location ~ .*\.(php|php5)$ {
          deny all; 
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log off;
    }

server
    {
        listen 443 ssl http2;
        #listen [::]:443 ssl http2;
        server_name static.example.com ;
        index index.html;
        root  /mnt/wwwroot/api.example.com/public;

        ssl_certificate /usr/local/nginx/conf/ssl/static.example.com/fullchain.cer;
        ssl_certificate_key /usr/local/nginx/conf/ssl/static.example.com/static.example.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
        ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
        ssl_session_cache builtin:1000 shared:SSL:10m;
        # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
        ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;

        include rewrite/none.conf;
        #error_page   404   /404.html;
        
        location / {
          #   指定允許跨域的方法,*代表所有
          add_header Access-Control-Allow-Methods *;
            
          #   預檢命令的緩存,如果不緩存每次會發送兩次請求
          add_header Access-Control-Max-Age 3600;
          #   帶cookie請求需要加上這個字段,並設置爲true
          add_header Access-Control-Allow-Credentials true;
            
          #   表示允許這個域跨域調用(客戶端發送請求的域名和端口) 
          #   $http_origin動態獲取請求客戶端請求的域   不用*的原因是帶cookie的請求不支持*號
          add_header Access-Control-Allow-Origin $http_origin;
            
          #   表示請求頭的字段 動態獲取
          add_header Access-Control-Allow-Headers 
          $http_access_control_request_headers;
            
          #   OPTIONS預檢命令,預檢命令通過時才發送請求
          #   檢查請求的類型是不是預檢命令
          if ($request_method = OPTIONS){
              return 200;
          }
        }
        
        location ~ .*\.(php|php5)$ {
          deny all; 
        }
        
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log off;
    }
  1. wx.example.com.conf
server
    {
        listen 80;
        #listen [::]:80;
        server_name wx.example.com ;
        index index.html;
        root  /mnt/wwwroot/wx.example.com;
        
        gzip_static on;
        
        include rewrite/none.conf;
        #error_page   404   /404.html;
        
        location ~ .*\.(php|php5)$ {
          deny all; 
        }
        
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log off;
        error_log /home/wwwlogs/wx.example.com_error.log;
    }

server
    {
        listen 443 ssl http2;
        #listen [::]:443 ssl http2;
        server_name wx.example.com ;
        index index.html;
        root  /mnt/wwwroot/wx.example.com;

        ssl_certificate /usr/local/nginx/conf/ssl/wx.example.com/fullchain.cer;
        ssl_certificate_key /usr/local/nginx/conf/ssl/wx.example.com/wx.example.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
        ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
        ssl_session_cache builtin:1000 shared:SSL:10m;
        # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
        ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;

        gzip_static on;
        
        include rewrite/none.conf;
        #error_page   404   /404.html;
        
        location ~ .*\.(php|php5)$ {
          deny all; 
        }
        
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log off;
        error_log /home/wwwlogs/wx.example.com_error.log;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章