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