nginx转发/重定向/反向代理

nginx转发

server {
    listen 80;
    server_name www.example.com;
    location / {
        proxy_pass http://127.0.0.1:8080;
        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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

nginx重定向

server {
	listen       80;
	server_name  www.test.com;
    location / {
        # 将所有请求重定向到 https
        return 301 https://$host$request_uri;
    }
}
server {
	listen       80;
	server_name  www.test.com;
	location /D220 {
		rewrite ^/D220$ https://www.example.com/
	}
}

nginx反向代理(解决跨域)

【本地 http://localhost:8081/api 配置跨域放行】

访问本地 http://localhost:8081/apihttp://127.0.0.1:8081/api

转发到 http://126.121.10.11:8080/wap/api/

server {
  listen       8081;
  server_name  localhost;
  location / {
          #root   html;
          alias   F:/vue/dist;
          index  index.html index.htm;
      }
  
  location /api{
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
    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';
    proxy_pass http://126.121.10.11:8080/wap/api/;
    proxy_set_header Origin $http_origin;
    proxy_set_header Access-Control-Request-Headers $http_access_control_request_headers;
    proxy_set_header Access-Control-Request-Method $http_access_control_request_method;
  }
}

你可能感兴趣的:(server,nginx,运维,服务器)