NGINX实现反向代理

时间:2023-03-10 06:16:32
NGINX实现反向代理

一、安装NGINX

略,请自行百度,GOOGEL

二、配置文件
1.由上面的步骤,我们看到配置文件放置在/etc/nginx/目录下:
主要配置文件:/etc/nginx/nginx.conf
扩展配置文件:/etc/nginx/conf.d/*.conf

user www-data;
worker_processes ;
pid /var/run/nginx.pid; events {
worker_connections ;
# multi_accept on;
} http { ##
# Basic Settings
## sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout ;
types_hash_max_size ;
# server_tokens off; # server_names_hash_bucket_size ;
# 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 ;
# gzip_buffers 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 /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

主配置文件的末尾,加载所有扩展配置文件里面以.conf结尾的文件。所以我们不要修改主要配置文件(不需要修改),用户配置都放到了/etc/nginx/conf.d/目录下,里面默认有两个配置文件,一个普通的配置,一个是ssl配置。

2.为一个域名配置一个文件(文件名任意,以.conf结尾即可)
#cd /etc/nginx/conf.d/
#vim www.test.com.conf
把内容修改如下:
---------------------------------------------------------------------------
server {
    listen       80;
    server_name  www.test.com;

charset utf8;
    access_log  /var/log/nginx/www.test.com.access.log  main;

location / {
        proxy_pass       http://192.168.1.20:80;
        proxy_set_header Host      $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
---------------------------------------------------------------------------

3.重启nginx服务,检查配置文件并生效(nginx -t)
#service nginx restart

4.如果没出意外,你应该已经成功实现Nginx反向代理服务了!

三、其他帮助
1.帮助命令
2.官方帮助文档:http://nginx.org/en/docs/