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;
  }
}

https访问端口映射

请求-->8000 -->443 -->8080

server {
        listen       8000;
        server_name  127.0.0.1;
        #rewrite ^(.*)$ https://$host$1 permanent;
        rewrite ^ https://$server_name$request_uri? permanent;
    }

    # HTTPS server
    server {
        listen       443 ssl;
        server_name  127.0.0.1;

        ssl_certificate      /usr/local/nginx/ssl/nginx.crt;
        ssl_certificate_key  /usr/local/nginx/ssl/nginx.key;

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

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

        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_pass http://127.0.0.2:8080;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值