昆山兼职做网站微信小程序开发平台官网
文章目录
- Nginx
- Nginx主要作用
- 转发配置
- 相关问题
- 参考
- 推荐阅读
Nginx
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,公开版本1.19.6发布于2020年12月15日
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名。2022年01月25日,nginx 1.21.6发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。
Nginx主要作用
- CI/DI
- 转发
- 负载均衡
本文主要内容:转发
转发配置
- tcp端口监听是唯一的,nginx要在哪个端口监听,是不能被占用的。配置一个server节点,就等于在server节点启动了一个tcp监听。
- nginx同一个端口可以监听多个不同host的请求。配置文件中可以重复写同一个端口的server节点。
#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;
}stream { server {listen 13389; #本地请求端口proxy_pass x.x.x.x:3389; #服务器远程桌面端口}server {listen 6672; #本地请求端口proxy_pass x.x.x.x:5672; #rabbitmq}}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 8888;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;#数采正式/测试接口 不走https了 不用配置跨域访问#location /RTP/DaqApi {# #Proxy Settings# add_header Access-Control-Allow-Origin $http_origin;# add_header Access-Control-Allow-Headers *;# add_header Access-Control-Allow-Methods *;# proxy_redirect off;# proxy_http_version 1.1;# proxy_set_header Host $host:$server_port;# proxy_set_header X-Real-IP $remote_addr;# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# proxy_set_header X-Forwarded-Proto $scheme; # proxy_set_header Upgrade $http_upgrade;# proxy_set_header Connection "upgrade"; # proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;# proxy_max_temp_file_size 0;# proxy_connect_timeout 90;# proxy_send_timeout 90;# proxy_read_timeout 90; # proxy_pass https://x.x.x.x:9433;#}location /RTPDaq/Api/{proxy_pass http://x.x.x.x:9433/; proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}#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中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
proxy_pass http://js.test.com/;
}
如上面的配置,如果请求的url是http://servername/static_js/test.html
会被代理成http://js.test.com/test.html
而如果这么配置
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
proxy_pass http://js.test.com;
}
则会被代理到http://js.test.com/static_js/test.htm
当然,我们可以用如下的rewrite来实现/的功能
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
rewrite /static_js/(.+)
/
1 break;
proxy_pass http://js.test.com;
}
参考
Nginx配置proxy_pass转发的/路径问题
推荐阅读
Nginx常用配置及代理转发