Nginx 内网反向代理服务器
安装 Nginx
取自nginx官网文档
下面为Centos安装操作,其他系统安装请看官方文档
- 安装基础组件
sudo yum install yum-utils
- 设置 yum 存储库,创建名为 /etc/yum.repos.d/nginx.repo 以下内容的文件
[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing。 key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx. org/keys/nginx_signing.key module_hotfixes=true
- 安装 Nginx
sudo yum install nginx
Nginx 常用命令
nginx #启动 nginx 服务
nginx -s stop #停止服务
nginx -s reload #重新载入配置,更改conf后需运行此命令
ps -ax | grep nginx #查看运行状态
nginx 反向代理配置
-
创建访问日志目录, 该目录下以 域名 区分各个网站的访问记录
mkdir /data/wwwlogs mkdir /data/www
-
进入 /etc/nginx/conf.d 目录,创建 www.domain.com.conf 文件并配置:
server { listen 80; # 修改域名 server_name www.domain.com; # 修改目录 root /data/www/www.domain.com; location / { # 这里修改为反向代理的 ip:port proxy_pass http://127.0.0.1:8080; 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_set_header REMOTE-HOST $remote_addr; add_header X-Cache $upstream_cache_status; add_header Cache-Control no-cache; } #禁止访问的文件或目录 location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) { return 404; } # 修改日志保存目录 access_log /data/wwwlogs/www.domain.com.log; error_log /data/wwwlogs/www.domain.com.error.log; }
-
保存,重载配置
sudo nginx -s reload
-
继续添加,将 www.domain.com 替换,重复执行第二步即可