nginx 反向代理的基本配置(以Ubuntu 16.04LTS为例)


nginx 反向代理的基本配置



1.安装nginx

pkill -9 apache2 # 关闭apache
sudo apt install nginx # 安装nginx,使用Ubuntu的包管理工具apt-get

2.修改apache 端口号,防止出错

sudo vim /etc/apache2/ports.conf # 讲端口修改81,防止和nginx 重复,导致重启失败
sudo service apache2 start # 启动apache 服务器,代理目标服务器

3.在nginx 目录下增加服务器配置

cd /etc/nginx/conf.d/ # 进入配置文件目录
vim host.conf # 创建自定义配置文件

写入host.conf 配置文件

server
{
listen 80;
server_name live.triste.com;
index index.html index.htm index.php
root /var/www/html;
location / {
proxy_pass http://localhost:81;
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;
proxy_temp_file_write_size 64k;
}
}

server
{
listen 80;
server_name info.triste.com;
index index.html index.htm index.php
root /var/www/html;
location / {
proxy_pass http://localhost:81;
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;
proxy_temp_file_write_size 64k;
}
}

重启nginx

sudo service nginx reload

效果图

nginx 访问

反向代理到apache

反向代理到apache


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com