nginx实现后台于前端代理

Posted 棘丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx实现后台于前端代理相关的知识,希望对你有一定的参考价值。

以下是nginx.conf文件配置信息

user root;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {

worker_connections 768;

}

http {

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

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

##
# SSL Settings
##

ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

upstream xccdz{

server 192.168.118.111:9021;

}

前段nginx代理

server {

listen       80;
server_name  192.168.118.111;    #该域名为阿里云绑定服务器的域名

add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Credentials true;

location / {
    root /home/guanghui/cdz/dist;             #项目路径
    index  index.html index.htm;               
}

location /auth/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://xccdz/;
}
location /gitweb.cgi {
    include fastcgi_params;
    gzip off;
    fastcgi_param   SCRIPT_FILENAME  /usr/share/gitweb/gitweb.cgi;
    fastcgi_param   GITWEB_CONFIG  /etc/gitweb.conf;
    fastcgi_pass    unix:/var/run/fcgiwrap.sock;
}

location /gitweb {
    root /usr/share/gitweb/;
    index gitweb.cgi;
}

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

}

二号监听

server

{
    listen 8013;
    server_name 192.168.118.111;
    index index.html;
    root  /home/guanghui/eladmin/dist;  #dist上传的路径

    # 避免访问出现 404 错误
    location / {
      try_files $uri $uri/ @router;
      index  index.html;
    }

    location @router {
      rewrite ^.*$ /index.html last;
    }

    # 接口
    location /api {
      proxy_pass http://127.0.0.1:8000;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-Port $server_port;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }

    # 授权接口
    location /auth {
      proxy_pass http://127.0.0.1:8000;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-Port $server_port;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }

    # WebSocket 服务
    location /webSocket {
      proxy_pass http://127.0.0.1:8000;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-Port $server_port;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }

    # 头像
    location /avatar {
      proxy_pass http://127.0.0.1:8000;
    }

    # 文件
    location /file {
      proxy_pass http://127.0.0.1:8000;
    }
}

}

mail {

See sample authentication script at:

http://wiki.nginx.org/ImapAut...

auth_http localhost/auth.php;

pop3_capabilities "TOP" "USER";

imap_capabilities "IMAP4rev1" "UIDPLUS";

server {

listen localhost:110;

protocol pop3;

proxy on;

}

server {

listen localhost:143;

protocol imap;

proxy on;

}

}

以上是关于nginx实现后台于前端代理的主要内容,如果未能解决你的问题,请参考以下文章

nginx反向代理-多端口映射

使用nginx将https请求代理为http

nginx 代理

nginx做反向代理负载均衡 Java怎么获取后端服务器获取用户IP

图文讲解,如何使用 Nginx 反向代理负载均衡

Nginx处理前端跨域(补充)