怎么配置Nginx以代理多个后台地址
发布网友
发布时间:2022-02-26 21:23
我来回答
共2个回答
懂视网
时间:2022-02-27 01:44
产品型号:Thinkpad E15
系统版本:centos8
nginx反向代理配置
安装nginx服务
[root@xuegod63 ~]# yum install -y epel-release #安装yum扩展源,扩展源中有nginx安装包
[root@xuegod63 ~]# yum install -y nginx
我们可以通过 proxy_pass 来配置反向代理
[root@xuegod63 ~]# vim /etc/nginx/nginx.conf
改:
47 location / {
48 }
为:
47 location / {
48 proxy_pass http://www.163.com;
49 }
[root@xuegod63 ~]# systemctl start nginx
查看IP地址
[root@xuegod63 ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.63 netmask 255.255.255.0 broadcast 192.168.1.255
访问web服务:http://192.168.1.63/
当我们访问192.168.1.63,就直接跳转到163网站了,说明我们配置的反向代理生效了。
总结:
1、安装nginx服务
[root@xuegod63 ~]# yum install -y epel-release #安装nginx yum源
[root@xuegod63 ~]# yum install -y nginx
2、我们可以通过 proxy_pass 来配置反向代理
[root@xuegod63 ~]# vim /etc/nginx/nginx.conf
改:
47 location / {
48 }
为:
47 location / {
48 proxy_pass http://www.163.com;
49 }
热心网友
时间:2022-02-26 22:52
nginx.conf的配置如下,这个是反向代理集群的配置文件。
#user nobody;
worker_processes auto;
error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request"'
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
# server_names_hash_bucket_size 128K;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_body_buffer_size 8m;
server_tokens off;
ignore_invalid_headers on;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
proxy_temp_path /usr/local/nginx-1.8/proxy_temp;
proxy_cache_path /usr/local/nginx-1.8/proxy_cache levels=1:2 keys_zone=cache_one:100m inactive=2d max_size=10g;
gzip on;
gzip_disable "MSIE [1-6].(?!.*SV1)";
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
upstream name {
server 116.31.118.114:8098 weight=2 fail_timeout=3s backup; ----代理地址集群
server 114.55.32.244:888 weight=1 max_fails=3 fail_timeout=300; ----代理地址集群
server 114.55.85.154:8080 weight=1 max_fails=3 fail_timeout=300; ----代理地址集群
ip_hash;
}
server {
listen 443 default ; ---监听端口
server_name www.***.com;
server_tokens off;
ssl on; ---https配置
ssl_certificate /usr/local/nginx/conf/web.crt; ---https配置
ssl_certificate_key /usr/local/nginx/conf/web.key; ---https配置
error_page 497 https://$host:$server_port$request_uri; ---https配置
location ~*/{
proxy_set_header Host $http_host; ---获取真实IP的
proxy_set_header X-Real-IP $remote_addr; ---获取真实IP的
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ---获取真实IP的
proxy_pass http://name; --反向代理
proxy_http_version 1.1;
proxy_set_header Accept-Encoding "";
location ~ .*\.(gif|jpg|png|html|css|js|ico|swf|pdf)(.*) { --缓存网站内容
proxy_pass http://name;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
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_cache cache_one;
add_header Nginx-Cache $upstream_cache_status;
proxy_cache_valid 200 304 301 302 24h;
proxy_cache_valid 404 1m;
proxy_cache_valid any 2d;
proxy_cache_key $host$uri$is_args$args;
expires 7d;
}
}
location ~ /purge(/.*)
{
auth_basic "TDT Center CACHE Center";
auth_basic_user_file /tmp/htpasswd;
allow 127.0.0.1;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
# error_page 404 /404.html;
# error_page 400 501 502 503 504 https://$host:$server_port$request_uri;
# location = /50x.html {
# root html;
# }
# redirect server error pages to the static page /50x.html
#
# error_page 500 502 503 504 /50x.html;
}
}