nginx之重定向配置和反向代理代理配置

一、nginx重定向(从服务器上访问前端静态资源)

示例配置:
如下配置在nginx的nginx.config文件中配置

    server {
    	#同时监听443端口和80端口,443是https,80是http
        listen 80 default;
        listen 443    ssl;
        #服务器域名
        server_name  dev.park.com;
        #ssl on;
        ssl_certificate   ../cert/6154857_dev.park.com.pem;
        ssl_certificate_key  ../cert/6154857_dev.park.com.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        
 		location /reserve/h5 {
            root   html;
            index  index.html index.htm;
         }
  }

将静态页面放在nginx的html文件夹下,当我们想要访问这/nginx/html/reserve/h5个路径下的index.html时,可以通过访问http://dev.park.com/reserve/h5,这样就实现了转发。
在这里插入图片描述

二、nginx反向代理

反向代理:例如访问A服务器想要访问C服务器,但是C不允许外网访问,这时我们就只能引入B服务器,让A先去访问B,然后让B去访问C,从而拿到A想要的东西。

示例配置:
如下配置在nginx的nginx.config文件中配置


    server {
    	#同时监听443端口和80端口,443是https,80是http
        listen 80 default;
        listen 443 ssl;
        #服务器域名
        server_name  47.park.com;
        ssl_certificate  ../cert/6568403_47.park.com.pem;
        ssl_certificate_key  ../cert/6568403_47.park.com.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        #charset koi8-r;
        
		location /reserve/officialAccount {
			proxy_pass http://localhost:8083;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout       600;
            proxy_read_timeout          600;
            proxy_send_timeout          600;
		}

这时我们访问http://47.park.com/reserve/officialAccount这个地址,实际上访问的真实地址是http://localhost:8083,localhost是nginx所在的服务器的ip地址。

总结

以上就是linux服务器上对ngnix实现重定向(从服务器上访问前端静态资源)和反向代理的配置。

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:1 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐