nginx配置正向代理

一、安装Nginx

服务器选用ubuntu,不做其他配置优化,仅作简单安装

#install Nginx
sudo apt-get install nginx

#检查服务配置文件
sudo nginx -t
#nginx: configuration file /etc/nginx/nginx.conf test is successful  表示配置文件符合标准配置,解析成功

#sudo service nginx {start|stop|status|restart|reload|configtest|}
#启动服务
sudo service nginx start

#停止服务
sudo service nginx stop

二、配置正向代理

1、添加服务器正向代理配置

/etc/nginx/nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}


http {

        ##
        # Basic Settings
        ##
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

        ##
        # Virtual Host Configs
        ##
        ##include proxy/*.conf;
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
        access_log  /data/httplogs/proxy-$host-aceess.log;      
}



#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
# 
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

只需要将server配置添加进http{…}内重启服务即可

#比较基本的配置方式
server {
        ##代理日志配置 off 表示关闭日志输出
        ##access_log /home/bingchenglin/logs/nginx/access.log;
        ##文件路径可用于监控代理的接入情况
        access_log off;
        ##配置服务端口
        listen 8090;                    
        location / {
                ##DNS地址 多个DNS地址用空格隔开
                resolver 192.168.241.2;
                ##环境变量通配一般不改
                proxy_pass $scheme://$http_host$request_uri;
                proxy_buffers   256 4k;                         
                proxy_max_temp_file_size 0k;                        
        } 
}

而实际不建议直接修改nginx.conf,去不去改动默认配置,由于默认配置存在以下通配型引入配置文件项

include /etc/nginx/conf.d/*.conf;

需要添加的server配置存放的/etc/nginx/conf.d/ 下的.conf目录下,可以自己新建一个目录然后在nginx.conf的http{…}中引入

#存放我配置信息
include proxy/*.conf

2、添加代理配置

#进入存放代理配置的目录(/etc/nginx/conf.d也可)
cd /etc/nginx/proxy
sudo vi myproxy.conf

#添加配置
server {
        access_log /home/bingchenglin/logs/nginx/access.log;            
        listen 8090;                    
        location / {
                resolver 192.168.241.2;
                proxy_pass $scheme://$http_host$request_uri;                            
                proxy_buffers   256 4k;                         
                proxy_max_temp_file_size 0k;                        
        } 
}

3、检查nginx.conf配置,重启服务

##如果是fail根据emerg修改配置信息
sudo nginx -t

##成功后重启
sudo service nginx start

三、测试代理是否配置成功

打开浏览器访问该IP能看的nginx

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

给浏览器添加代理服务器

IE添加方法:Interne选项->连接->局域网设置->代理服务器->添加代理服务器的IP和监听端口信息->确定
浏览网页

服务端监控

tail -f /home/bingchenglin/logs/nginx/access.log

你可能感兴趣的:(linux,shell,nginx)