Windows XP Windows 7 Windows 2003 Windows Vista Windows教程綜合 Linux 系統教程
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟件教程
 Windows教程網 >> Linux系統教程 >> Linux教程 >> Nginx配置反向代理服務器

Nginx配置反向代理服務器

日期:2017/2/7 14:36:36      編輯:Linux教程
 

nginx是一個高性能的web服務器,它也是一個很強的反向代理服務器。我們只要下載源碼編譯安裝配置就可以了。

一、安裝Nginx
1、安裝所需的PCRE庫:

cd /tmp
wget -c ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.13.tar.gz
tar -zxvf pcre-8.13.tar.gz
cd pcre-8.13
./configure --prefix=/usr
make
make install

2、下載nginx源碼安裝:

cd /tmp
wget -c http://nginx.org/download/nginx-1.0.8.tar.gz
tar -zxvf nginx-1.0.8.tar.gz
cd nginx-1.0.8
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_addition_module --with-http_perl_module --with-http_flv_module --with-http_gzip_static_module --with-http_realip_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_dav_module
make
make install

3、建立nginx用戶:

useradd nginx -s /sbin/nologin -M

4、以下是安裝後顯示的一些安裝路徑:
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

二、配置nginx
1、編輯配置文件/usr/local/nginx/conf/nginx.conf

vim /usr/local/nginx/conf/nginx.conf

2、修改以下內容
將第一行user前的#去掉,改為:

user nginx nginx;

將worker_processes改大,比如設為2:

worker_processes 2;

將以下兩行前的#去掉:

error_log logs/error.log
pid logs/nginx.pid

綁定用來代替的域名或IP:

listen 80;
server_name 綁定的域名或IP;

3、配置反向代理內容,並使用HttpSubModule替換URL:
找到
location / {
root html;
index index.html index.htm;
在後加入:

sub_filter 要反向代理的網址 代替的域名或IP; #替換URL
sub_filter_once off; #搜索替換全部行
proxy_pass http://要反向代理的網址;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Accept-Encoding ""; #清除編碼

3、編寫nginx啟動腳本,設置開機自啟動:

vim /etc/init.d/nginx

啟動腳本內容看這裡

chmod 755 /etc/init.d/nginx
chkconfig --level 345 nginx on

4、測試反向代理
浏覽器輸入反向代理的域名或IP,查看能夠正常打開,URL是否替換。

Copyright © Windows教程網 All Rights Reserved