例1
目前就稳定性来说,Apache是没得比的。因此,用nginx做反向代理比较合适。
这里是给http://172.30.170.8:8088/vod/做代理,反向代理服务器的名称为vod.xx.xxx.cn ,监听80端口。
Apache httpd服务器监听8088端口(我这里apache与反向代理服务器在同一服务器)。
全局配置参数做些调整:
代码如下 | 复制代码 |
hacklog@hywd:/etc/nginx$ cat nginx.conf user www-data; worker_processes 4; error_log /var/log/nginx/error.log; events { http { access_log /var/log/nginx/access.log; gzip on; server_names_hash_bucket_size 256; #size limits include /etc/nginx/conf.d/*.conf; |
然后是反向代理配置了,有些参数必须调整,如client_max_body_size 不调整的话,通过web上传东西会有问题:
代码如下 | 复制代码 |
hacklog@hywd:/etc/nginx/sites-available$ pwd /etc/nginx/sites-available hacklog@hywd:/etc/nginx/sites-available$ cat proxy_local_apache # You may add here your # server { # ... # } # statements for each of your virtual hosts server { access_log /var/log/nginx/vod.xx.xxx.cn.access.log; location ~ ^/status/ { location / { proxy_pass http://172.30.170.8:8088/vod/; location /doc {
#error_page 404 /404.html; hacklog@hywd:/etc/nginx/sites-available$ |
nginx日志切割脚本
代码如下 | 复制代码 |
root@hywd:/etc# cat /usr/local/sbin/cut_nginx_log.sh #!/bin/bash # This script run at 00:00 # The Nginx logs path mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/ crontab -e |
例2
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 |
启动脚本
代码如下 | 复制代码 |
#!/bin/sh # # nginx - this script starts and stops the nginx daemin # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # config: /usr/local/nginx/conf/nginx.conf # pidfile: /usr/local/nginx/logs/nginx.pid # Source function library. # Source networking configuration. # Check that networking is up. nginx="/usr/local/nginx/sbin/nginx" NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" lockfile=/var/lock/subsys/nginx start() { stop() { restart() { reload() { force_reload() { configtest() { rh_status() { rh_status_q() { case "$1" in |
设置权限
代码如下 | 复制代码 |
chmod 755 /etc/init.d/nginx chkconfig --level 345 nginx on |