nginx代理配置问题,求大佬指教 2C

我想把我本地nginx服务配置成代理服务器,
需要代理的服务地址是http://192.168.153.128:8080/,
我想把它代理成http://localhost/demo/,应该如何配置nginx。

3个回答

nginx 代理服务比较简单,

 location / {
            proxy_pass   http://127.0.0.1:8081;
        }

参考资料:https://blog.yoodb.com/yoodb/article/detail/1243

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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"';

    log_format  porxy  '$http_x_forwarded_for - $remote_user  [$time_local]  '
                             ' "$request"  $status $body_bytes_sent '
                             ' "$http_referer"  "$http_user_agent" ';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip on;
    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/javascript text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary off;
    gzip_disable "MSIE [1-6]\.";

    #服务名对应的服务器列表


    #代理服务
    server {
        listen   8080; #侦听端口
        server_name 192.168.153.128; #侦听服务名
        #日志路径
        access_log logs/access.log;
        error_log logs/error.log info;
        #拦截正则表达式
        location / {
            proxy_pass_header Server;
            #proxy_set_header Host $http_host;
            proxy_set_header Host 'localhost';
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://localhost/demo/;
        }     
    }

}

中间serve开始的那段是关键,更多的你可以去菜鸟上面学一下nginx配置

#定义Nginx运行的用户和用户组 如果对应服务器暴露在外面的话建议使用权限较小的用户 防止被入侵
#user nobody;

#Nginx 进程数,建议设置为等于CPU总核心数
worker_processes 1;

#开启全局错误日志类型
error_log logs/error.log info;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

#一个Nginx进程打开的最多文件面熟数目 建议与ulimit -n 一致
#如果面对高并发时 注意修改该值 ulimit -n 还有部分系统参数 而并非这个单独确定r
worker_rlimit_nofile 1024;
events {
use epoll;
worker_connections 1024;
}

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;
#sendfile可以让senfile()发挥作用。可以在磁盘和tcp socket之间互相拷贝数据
sendfile        on;
#告诉nginx在一个数据包里发送所有头文件,二不是一个接一个的发送 
tcp_nopush     on;
#告诉nginx不要缓存数据,而是一段一段的发送
tcp_nodelay   on;
#给客户端分配keep-alive连接超时的时间
keepalive_timeout  65;
types_hash_max_size 2048;

#gzip  on; 压缩传输
gzip on;
gzip_min_length 1k;
gzip_buffers 19 64k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml application/javascript;
gzip_vary on;

#配置代理参数
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;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 65;
proxy_send_timeout 65;
proxy_read_timeout 65;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;

#缓存配置
proxy_cache_key '$host:$server_port$request_uri';
proxy_temp_file_write_size 64k;

# proxy_temp_path /dev/shm/JieLiERP/proxy_temp_path;
# proxy_cache_path /dev/shm/JieLiERP/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=5d max_size=1g;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
upstream test {
# ip_hash;

# 负载均衡设置,weight越大越优先分配
server 192.168.153.128:8080;

} 

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        proxy_pass http://test;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;

#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;

#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

}

你可以试一下这样。这样就可以代理。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问