Nginx
Core
Location
See https://nginx.org/en/docs/http/ngx_http_core_module.html#location
通过server_name
绑定多域名abc.com
和api.abc.com
。前端访问/
直接代理到后端index.html
,前端访问a.html
代理到后端a.html
。
server {
listen 80;
server_name abc.com *.abc.com;
location = / {
proxy_pass 11.22.33.44:80/index.html;
}
location / {
proxy_pass 11.22.33.44:80;
}
}
将www.zhuoludian.com域名永久重定向到landingsite.cn
server {
listen 80;
server_name zhuoludian.com *.zhuoludian.com;
return 301 http://landingsite.cn$request_uri;
}
Nginx if
https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if
- a variable name; false if the value of a variable is an empty string or “0”;
Before version 1.0.1, any string starting with “0” was considered a false value. - comparison of a variable with a string using the “=” and “!=” operators;
matching of a variable against a regular expression using the “~” (for case-sensitive matching) and “~” (for case-insensitive matching) operators. Regular expressions can contain captures that are made available for later reuse in the $1…$9 variables. Negative operators “!~” and “!~” are also available. If a regular expression includes the “}” or “;” characters, the whole expressions should be enclosed in single or double quotes.
checking of a file existence with the “-f” and “!-f” operators;
checking of a directory existence with the “-d” and “!-d” operators;
checking of a file, directory, or symbolic link existence with the “-e” and “!-e” operators;
checking for an executable file with the “-x” and “!-x” operators.
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
set $id $1;
}
if ($request_method = POST) {
return 405;
}
if ($slow) {
limit_rate 10k;
}
if ($invalid_referer) {
return 403;
}
#user root;
events {
}
http {
server {
listen 80;
server_name localhost;
rewrite_log on;
location / {
set $first_language, $language_suffix;
if ($http_accept_language ~* '^(.+?),') {
set $first_language $1;
}
set $language_suffix 'en';
if ($first_language ~ '^zh(\-CN?)') {
set $language_suffix 'zh';
}
rewrite ^(.*)$ /$language_suffix/$1 break;
proxy_pass http://localhost:3000;
}
location /api/backend {
rewrite ^/api/backend/(.*)$ /$1 break;
proxy_pass http://localhost:3100;
}
}
}