ubuntu 上配置反向代理 Nginx

By | 2018-11-26

ubuntu 安装步骤是非常简单

apt-get install nginx

具体可参考以下官方网址:
反向代理配置:
例1: 用nginx 作为代理多台服务器及多网址的反向代理
内网有三个网站,分别存放于三台服务器上
a.xxx.com     http://192.168.10.10:8088
b.xxx.com     http://192.168.10.20:8088
c.xxx.com     http://192.168.10.30:8088
本例以每个网址一个配置文件来说明:
在 /etc/nginx/conf.d 目录增加三个配置文件
a.xxx.com.conf
b.xxx.com.conf
c.xxx.com.conf
内容如下(a/b/c 三个网址的配置相同,不同处是以下字部分):

## Basic reverse proxy server ##
## Apache backend for a.xxx.com ##

## Start a.xxx.com ##
server {
listen 80; <– 这个端口为 Nginx 反向代理监听端口,要与后端服务器的服务端口不同
server_name a.xxx.com;
access_log /var/log/nginx/a.access.log;
error_log /var/log/nginx/a.error.log;
root html;
index index.html index.htm index.php;

## send request back to apache ##
location / {
proxy_pass http://192.168.10.10:8088/; <– 指向后端服务器的网址

#Proxy Settings
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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;

}

}

## End a.xxx.com ##
第二种方法是多域名配置写于一个配置文件内:

一个文件添加多个域名的规则也是一样,只要把上面单个域名重复写下来就ok了,如:

 

server
{
listen       80;
server_name a.xxx.com;             #绑定域名
index index.htm index.html index.php;      #默认文件
root /home/www/a.xxx.com;               #网站根目录
include location.conf;                            #调用其他规则,也可去除
}

 

server
{
listen       80;
server_name b.xxx.com;             #绑定域名
index index.htm index.html index.php;      #默认文件
root /home/www/b.xxx.com;        #网站根目录
include location.conf;                            #调用其他规则,也可去除
}

以下配置方法未验证:

三、不带www的域名加301跳转

如果不带www的域名要加301跳转,那也是和绑定域名一样,先绑定不带www的域名,只是不用写网站目录,而是进行301跳转,如:

server

{
listen 80;
server_name xxx.com;
rewrite ^/(.*) http://www.xxx.com/$1 permanent;
}

 

 

四、添加404网页

添加404网页,都可又直接在里面添加,如:

server

{
listen       80;
server_name www.xxx.com;             #绑定域名
index index.htm index.html index.php;      #默认文件
root /home/www/xxx.com;               #网站根目录
include location.conf;                            #调用其他规则,也可去除
error_page 404  /404.html;
}

 

最后还有一个方法需要注意,可能有需要禁止IP直接访问80端口或者禁止非本站的域名绑定我们的IP,这样的话应该

如下处理,放到最前一个server上面即可:

 

server{

listen   80 default;

server_name      _;

return 403;

}

最后要记住: 配置文件要生效必须重启 nginx

#/etc/init.d/nginx restart

————————– 配置文件中 proxy_redirect 的语法 ————————–
出处:http://nginx.179401.cn/StandardHTTPModules/HTTPProxy.html
proxy_redirect
语法:proxy_redirect [ default|off|redirect replacement ]
默认值:proxy_redirect default
使用字段:http, server, location
如果需要修改从被代理服务器传来的应答头中的”Location”和”Refresh”字段,可以用这个指令设置。
假设被代理服务器返回Location字段为: http://localhost:8000/two/some/uri/
这个指令:
proxy_redirect http://localhost:8000/two/ http://frontend/one/;
将Location字段重写为http://frontend/one/some/uri/。
在代替的字段中可以不写服务器名:

proxy_redirect http://localhost:8000/two/ /;
这样就使用服务器的基本名称和端口,即使它来自非80端口。
如果使用“default”参数,将根据location和proxy_pass参数的设置来决定。
例如下列两个配置等效:

location /one/ {  proxy_pass       http://upstream:port/two/;  proxy_redirect   default;} location /one/ {  proxy_pass       http://upstream:port/two/;  proxy_redirect   http://upstream:port/two/   /one/;}
在指令中可以使用一些变量:

proxy_redirect   http://localhost:8000/    http://$host:$server_port/;
这个指令有时可以重复:

proxy_redirect   default;  proxy_redirect   http://localhost:8000/    /;  proxy_redirect   http://www.example.com/   /;
参数off将在这个字段中禁止所有的proxy_redirect指令:

proxy_redirect   off;  proxy_redirect   default;  proxy_redirect   http://localhost:8000/    /;  proxy_redirect   http://www.example.com/   /;
利用这个指令可以为被代理服务器发出的相对重定向增加主机名:

發佈留言