nginx配置HTTP和WebSocket

随着前后端分离式开发越来越受欢迎,随之而来的前后端服务连接的问题也更加突出。

既然前后端服连接上存在一些问题,那么就会有针对这些问题的一系列的解决方案,而其中最为优秀的一种就是nginx服务代理。

目录

Nginx 默认配置

Nginx 配置 HTTP

Nginx 配置 WebSocket 


首先介绍一下nginx的HTTP服务代理。

前端开发的网页基本上是静态资源文件,甚至可以直接通过打开文件的方式进行访问html页面,而后端开发的API接口则必须通过服务才能连接,而前端页面所需要的数据都来自于后端提供的API接口,如果想要一个网页正常运行,则前后端配合开发是必不可少的环节。由于前端开发的页面与后端开发的API不再同一个服务下面,前端要想直接连接后端API服务就会出现跨域问题。当然解决跨域的方案有很多,这里就不一一赘述了,我们要说的通过nginx配置来解决前后端开发在数据资源连接上的问题,下面要说的是发布环境上的前后端资源连接问题,至于开发环境上的前后端数据资源连接上的问题,网络上有很多,而且很多脚手架工具均有对应的解决方案,这里不再赘述。

Windows发布环境与Linux发布环境上的nginx配置基本上大同小异。

Nginx 默认配置

要配置nginx配置,首先要找到nginx的配置文件,以windows为例,一般情况下nginx的配置文件默认会在安装路径下 /nginx/conf/nginx.conf 。


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

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #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;
    #    }
    #}

}

 启动nginx只需要双击 nginx.exe 文件即可,然后在浏览器地址栏内输入http://localhost/ 即可看到 nginx 默认的 index.html 如下图所示:

nginx默认启动页面
nginx默认启动页面

Nginx 配置 HTTP

worker_processes  1;

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"';
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
}

Nginx 配置 WebSocket 

# ngxin.conf

# user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    # 自定义变量 $connection_upgrade
    map $http_upgrade $connection_upgrade {
        default          keep-alive;  # 默认为keep-alive 可以支持 一般http请求
        'websocket'      upgrade;     # 如果为websocket 则为 upgrade 可升级的。
    }
    
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location /websocket/ {
            proxy_http_version 1.1;
            proxy_read_timeout 360s;
            proxy_redirect off;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Upgrade $http_upgrade; # 此处配置 上面定义的变量
            # proxy_set_header Connection $connection_upgrade;
            proxy_set_header Connection "Upgrade";    #配置连接为升级连接
        }
    }
}

 未完待续。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值