nginx反向代理解决网域名转发或跨域问题1

nginx反向代理解决网域名转发或跨域问题1

进行如下步骤:
1、下载nginx和相关软件包,进行安装;
下载地址:http://nginx.org/download/nginx-1.6.2.tar.gz
2、Nginx 配置
创建 Nginx 运行使用的用户 可以自己编写这里localhost:

user nobody;

worker_processes 1;

error_log logs/error.log;

error_log logs/error.log notice;

error_log logs/error.log info;

pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

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

    error_page  404              /index.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    # 匹配 api 路由的反向代理到tomcat
    location /api {

  proxy_pass http://192.168.10.227:8080/picms;
      proxy_set_header    REMOTE-HOST $remote_addr;
      proxy_set_header   Host $host;
      proxy_set_header   X-Real-IP $remote_addr;
      proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

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


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

#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.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;
#    }
#}

}
3、这里可以放多域名:
upstream monitor_server {
server 192.168.0.131:80;
server 192.168.0.132:80;
}

server
{
listen 80;
server_name localhost;
location / {
proxy_redirect off;
proxy_set_header Host h o s t ; p r o x y s e t h e a d e r X R e a l I P remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://monitor_server;
}
access_log logs/localhost_access.log;
}
4、启动问题
启动 Nginx
有问题可以一起交流:js\h5\java\php\c#技术一群 群号:289538443

猜你喜欢

转载自blog.csdn.net/weixin_42727360/article/details/81414345