【NGINX】反向代理、跨域配置、SSL证书配置

PythonNew_Mr.Wang / 2023-09-01 / 原文

反向代理

# 127.0.0.1 和 0.0.0.0 的区别
server {

  # 127.0.0.1 是一个特殊的IP地址,通常称为 "localhost" 或 "loopback" 地址。它指向本地计算机上的网络接口(即回环接口),用于在本地计算机内部进行通信。当你将请求代理到 127.0.0.1 时,nginx会将请求发送到本地计算机上,而不会通过网络发送出去。
  
  # 0.0.0.0 是一个特殊的IP地址,通常称为 "通配符地址"。它指示系统监听所有可用的网络接口上的请求。当你将请求代理到 0.0.0.0 时,nginx会尝试将请求发送到所有可用的网络接口上。但是由于 0.0.0.0 并不是一个具体的网络接口,所以nginx无法正确地将请求发送到指定的位置。
  
  location / {
      proxy_pass http://服务器公网IP:服务端口;
  }

  # 总结:使用docker容器nginx部署时,设置本地IP地址是无效的,直接配置服务器公网IP地址,使用代理服务器是无法看到请求IP:PROT
}



# proxy_pass中配置 " / " 有什么区别
server {
	
  # 在只有location / 是情况下,没有/会自动加上/;
  location / {
    proxy_pass http://服务器公网IP:服务端口;
  }
  
  # proxy_pass http://127.0.0.1:8080/;将访问完整的URL;
  # proxy_pass http://127.0.0.1:8080;将访问取出路径部分的URL;
  location /abc {
    proxy_pass http://服务器公网IP:服务端口/;
  }

  # 总结:在配置路径访问时,在proxy_pass的接口路径加上/;

}

跨域配置

# 所有域名都允许跨域
location / {

    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
    add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
    
    if ($request_method = 'OPTIONS') {
        add_header Access-Control-Allow-Credentials 'true';
        add_header Access-Control-Max-Age 1728000;
        add_header Content-Type 'text/plain charset=UTF-8';
        add_header Content-Length 0;
        return 204;
    }
}



# 指定域名允许跨域
location / {

    if ($http_origin = "http://www.baidu.com" || $http_origin = "http://www.demo.com") {
        add_header Access-Control-Allow-Origin $http_origin;
        add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
        add_header Access-Control-Allow-Headers "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range";
        add_header Access-Control-Allow-Credentials "true";
        add_header Access-Control-Max-Age 1728000;
    }
    if ($request_method = 'OPTIONS') {
        add_header Access-Control-Allow-Origin $http_origin;
        add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
        add_header Access-Control-Allow-Headers "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range";
        add_header Access-Control-Allow-Credentials "true";
        add_header Access-Control-Max-Age 1728000;
        add_header Content-Type "text/plain charset=UTF-8";
        add_header Content-Length 0;
        return 204;
    }
}

SSL证书配置

# 1:先申请免费证书后,下载证书:Nginx(适用大部分场景)(pem文件、crt文件、key文件)
# 2:这里要注意的是如果使用docker,一定要映射443端口,否则会导致无法访问
# 3:一旦修改成HTTPS进行处理请求,那么location的配置全部都需要更新到SSL里面


# HTTP的所有请求都重定向到HTTPS
server {
    listen       80;
    server_name  localhost  www.域名.com;
    return 301 https://$server_name$request_uri;
}

# 配置SSL:
server {
    listen       443 ssl;
    server_name  localhost  www.域名.com;
	
    # .crt和.pem文件都可以包含SSL证书和私钥的信息,都可以实现相同的功能
    # ssl_certificate      文件绝对路径.crt;
    ssl_certificate      文件绝对路径.pem;
    ssl_certificate_key  文件绝对路径.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
   	  root   html;
      index  index.html index.htm;
    }

    location /api {
        # 针对/api路径的配置
        # ...
    }

    location /static {
        # 针对/static路径的配置
        # ...
    }
}