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/api 或 http://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;
}
}