配置Nginx作为反向代理服务器

最近在实习公司的开发一个项目,项目是前后端彻底分离的项目,前端项目和后端项目各监听着特定的端口号,显然不是80的通用端口,为了不在地址栏上输入IP+端口号的形式,我们可以使用Nginx作为反向代理服务器,达到人性化访问的目的。

一,设置hosts

当然首先别忘了设置hosts,把你想要的域名设置进去,设置方法如下:

 

cd /etc
sudo vi hosts 
//hosts文件
//添加示例如下:
127.0.0.1 www.example.com

 

这个时候你访问www.example.com就是访问的127.0.0.1:8080这个url

好了,设置好了hosts,我们需要配置Nginx反向代理服务器的配置。

二,配置Nginx反向代理服务器

首先我们进入Nginx的目录。

cd /usr/local/etc/nginx

我们会发现这个目录下有个nginx.conf的配置文件

#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       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

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

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # 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;
    #    }
    #}
    include servers/*;//主要看这里,这里的配置说明了除了可以在这里配置Nginx的配置外,还可以在当前目录下的servers子目录中配置。
}

那么我们去servers目录下配置我们的属性。

upstream example_server_name {
    server localhost:8081;   //这里配置我们的前端地址,其实这里可以配置多个地址那么Nginx做的不光是反向代理的功能了,还能实现负载均衡的功能。
}

这样设置好了我们的前端地址,那么我们需要把访问hosts设置的地址转向访问我们上面设置的前端地址。

 

   server { listen 80; //这里国际默认通用80端口
    server_name  www.example.com *.example.com;//hosts中设置的访问地址

    # individual nginx logs for this web vhost
    access_log  /tmp/rrs-access.log;
    error_log   /tmp/rrs-error.log ;

    location = /favicon.ico {
       return  404;
    }

    #when not specify request uri, redirect to /index;
    location = / {
       rewrite ^ /index ;
    }


    #static files
    location ~/(.*)$ {
       root x/xx/xx; //静态资源访问
       expires 30d;
       access_log off;
    }

location / {

   proxy_pass http://example_web_server; //配置转向的upstream,就是最上面设置的upstream。

   proxy_set_header X-Real-IP $remote_addr;

   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_set_header Host $http_host;

   proxy_set_header X-Mobile-Client $mobile_request;
  }

}

这个时候Nginx反向代理服务器的配置就已经配置完成了,我们可以访问www.example.com验证一下Nginx代理是否成功。

三,Nginx命令

sudo nginx //启动Nginx

sudo nginx -s reload //重启nginx

sudo nginx -s quit //退出nginx

 

我也是刚接触到Nginx很多东西也不是很熟悉,慢慢学习以后不断补充......

 

 

 

 

 

 

 

 

 

 

posted @ 2016-11-23 10:32  说话的方式简单点丶  阅读(16060)  评论(0编辑  收藏  举报