[ nginx配置文件 ]


路由匹配规则

案例1:

输入: 127.0.0.1:8000/api/chf/

location ^~/api/ {
    proxy_pass http://11.11.11.11:8000/;
}

输出: http://11.11.11.11:8000/chf

案例2:

输入: 127.0.0.1:8000/api/chf/

location ^~/api {
    proxy_pass http://11.11.11.11:8000;
}

输出: http://11.11.11.11:8000/api/chf/

配置文件

default.conf

server {
    listen       443 ssl;
    #listen  [::]:443;
    server_name  frp.chfwork.cloud;
    ssl_certificate      /dist_ssl/tls.crt;
    ssl_certificate_key  /dist_ssl/tls.key;

    ssl_session_timeout 5m;   
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html/vue;
        try_files $uri $uri/ @router;
        index  index.html index.htm;
    }
    location @router {
        rewrite ^.*$ /index.html last;
    }
    location ^~/api/ {
        proxy_pass http://shopping-auth-python.chf-object:8000/api/;
    }
    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

直接在浏览器上面返回文字或者文件

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
  worker_connections  1024;
}


http {
  include /etc/nginx/mime.types;
  default_type  application/octet-stream;

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

  access_log  /var/log/nginx/access.log  main;

  sendfile        on;
  #tcp_nopush     on;

  keepalive_timeout  65;

  gzip on;
  server {
    listen 80;
    keepalive_timeout 70;
    location / {
      # 直接在浏览器显示文字
      add_header Content-Type text/plain;
      return 200 "index ~~~~!!!!\n 哈哈哈1 \n 哈哈哈2 \n 垃圾3";
    }
    location ^~/test1 {
      # 次情况默认会下载文件
      return 200 "HTTP/1.1 200 OK";
    }
    location ^~/test2 {
      add_header Content-Type text/plain;
      return 200 "HTTP/1.1 200 OK";
    }
    location /json {
        # 设置文件路径
        root /test;

        # 直接返回 JSON 文件
        try_files /test.json =404;

        # 强制返回 JSON 格式的响应
        add_header Content-Type application/json;
    }
  }
}