Server {Listen the; server_name xxx.com; # Add_header'*' ; Location/u/{# reverse proxy pass-through client IP proxy_set_header X-real-IP $remote _addr; Proxy_set_header X-forwarded-For $proxy _add_x_forwarded_for; Proxy_set_header Host $http _host; Proxy_set_header X-nginx-proxytrue; Proxy_pass http://127.0.0.1:8001;Proxy_redirect off; } Location/c/{proxy_pass http://127.0.0.1:8002;# webscoket Support Proxy_http_version1.1; Proxy_set_header Upgrade $http _upgrade; Proxy_set_header Connection"Upgrade"; Proxy_set_header Origin xxx; Proxy_set_header X-real-IP $remote _addr; Proxy_set_header X-forwarded-For $proxy _add_x_forwarded_for; Proxy_set_header Host $http _host; Proxy_set_header X-nginx-proxytrue; }}
Back up a wave of this configuration first.
First, set the listening port for the server, and then the domain name of the service.
The specific usage of location can be described in detail in the second article of reference. Here I use/u/to match my path with the configuration in the following {} under the/u/start.
I'll describe each parameter in turn:
Proxy_set_header x-real-IP $remote _addr; # Remote Real IP address proxy_set_header X-forwarded-for$proxy _add_x_forwarded_for; # The reverse proxy forwards the previous IP address proxy_set_header Host $http _host; # HTTP request Host domain name Proxy_set_header X true; # Nginx Agent
These parameters are used in the Nginx reverse proxy.
Proxy_pass http://127.0.0.1:8001; # Reverse proxy forwarding address proxy_redirect off; # whether the proxy supports redirection
The following matches the/c/path, which is my websocket forwarding service. Unlike the above, he needs to add the following to the header.
1.1"upgrade";
Upgrade http1.1 to WebSocket protocol. Another particular note is that if the WebSocket server receives the WebSocket handshake package, the origin information does not match the domain information. The service will be denied directly. This is very pit, I spent nearly half a day to find out why not even the reason, finally found that Origin must and request address in a domain, otherwise it will be denied access and return 403.
In addition, all the variables in the above configuration can be obtained and acquired through the information inside the Get_header.
Depending on the different want more detailed view of the configuration can check Nginx website and reference.
Above.
Reference:
https://segmentfault.com/n/1330000006925038 nginx configuration file Grooming
Http://seanlook.com/2015/05/17/nginx-location-rewrite/nginx Configuration Location Summary and rewrite rule notation
Http://www.ha97.com/5194.html (Summary) nginx configuration file nginx.conf Chinese explanation
http://nginx.org/en/docs/http/websocket.html WebSocket proxying
Nginx Server Configuration/websocket nginx configuration Note