如何在NGINX配置中为两个位置使用相同的规则?
我尝试了以下内容
server {
location /first/location/ | /second/location/ {
..
..
}
}
Run Code Online (Sandbox Code Playgroud)
但是nginx reload抛出了这个错误:
nginx: [emerg] invalid number of arguments in "location" directive**
Run Code Online (Sandbox Code Playgroud) 有什么区别:
location = /abc {}
Run Code Online (Sandbox Code Playgroud)
和
locaton ~ /abc {}
Run Code Online (Sandbox Code Playgroud) Nginx 正则表达式位置语法
正则表达式可以与 Nginx 位置块部分一起使用,这是通过 PCRE 引擎实现的。
由于没有完整记录,此功能究竟支持什么?
我正在尝试优化我的"位置"指令,并且找不到确定是否尝试特定位置匹配的好方法.使用echo位置块内不帮助这里.
NGINX ngx_http_core_module文档有点令人困惑.
要使用正则表达式,必须使用前缀:
~ 区分大小写的匹配
~* 对于不区分大小写的匹配
如何进行比赛:
具有=与查询完全匹配的前缀的指令.如果找到,搜索停止.
所有剩余的指令与传统的字符串.如果此匹配使用^~前缀,则搜索停止.
正则表达式,按照在配置文件中定义的顺序.
如果#3产生匹配,则使用该结果.否则,使用#2的匹配.
这里的数字2表示"常规字符串",但后来说它可以与^~前缀一起使用.并不~意味着RegExp?如果没有,它如何确定什么不是RegExp?
具体来说,我想要以下内容:
/assets直接提供任何文字.停止搜索.
\.php$|/$通过快速CGI STOP SEARCH提供与RegExp匹配的任何内容.
通过文字直接提供其他所有内容 /
这样,只有/匹配尝试从资源外部提供的非动态文件.
我有:
location ^~ /assets {} # search-terminating literal? or regex?
location ~ \.php$|/$ {}
location / {} # is this match always attempted?
Run Code Online (Sandbox Code Playgroud)
从文档中,看起来实际的顺序是1-3-2,始终运行文字/匹配.是的,这种优化不会对实际性能产生任何影响,但我只是想澄清一些含糊之处.
我有一个在docker容器内运行的Angular2/Angular应用程序,并使用nginx来提供它.所以我的app base =/myapp /.使用基本网址(即www.server.com/myapp或www.server.com/myapp/)访问应用时,一切正常
events {
worker_connections 4096; ## Default: 1024
}
http {
include /etc/nginx/conf/*.conf;
server {
listen 80 default_server;
root /usr/share/nginx/html;
index index.html index.htm;
include /etc/nginx/mime.types;
underscores_in_headers on;
location /myapp {
# If you want to enable html5Mode(true) in your angularjs app for pretty URL
# then all request for your angularJS app will be through index.html
try_files $uri /myapp/index.html;
}
#Static File Caching. All static files with the following extension will be cached for 1 day
location …Run Code Online (Sandbox Code Playgroud) 下面是Jenkins的nginx配置文件.其中大部分完全按照我在文档中读到的那样.
配置文件:
upstream app_server {
server 127.0.0.1:8080 fail_timeout=0;
}
server {
listen 80;
listen [::]:80 default ipv6only=on;
server_name sub.mydomain.net;
location ^~ /jenkins/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server;
break;
}
auth_basic "[....] Please confirm identity...";
auth_basic_user_file /etc/nginx/.htpasswd;
}
Run Code Online (Sandbox Code Playgroud)
}
当导航到http://sub.mydomain.net/jenkins得到提示我为我的基本身份验证服务器说:[...]请确认识别....
这是正确的,但是一旦我输入正确的凭据,我就会再次获得基本身份验证的提醒,但这一次:服务器说:Jenkins.
第二个隐藏的basic_auth来自哪里?!这对我没有任何意义.
击中取消对第一个提示我,然后正确地接收401授权所需的错误.
在第二个基本身份验证中击中CANCEL("服务器说:Jenkins")我得到:
HTTP ERROR 401
Problem accessing /jenkins/. Reason:
Invalid password/token …Run Code Online (Sandbox Code Playgroud) Nginx 使用 PCRE 引擎来评估正则表达式,文档指出不使用分隔符,因此我们不必像在标准正则表达式中那样/转义 URI 中的正斜杠。/有效的 nginx 正则表达式的示例是location ~* /myapp/.+\\.php$
但以下代码转义了正斜杠
\n\nlocation ~ ^\\/(?:index|core\\/ajax\\/update|ocs\\/v2|ocm-provider\\/.+)\\.php(?:$|\\/)
在这种情况下到底意味着什么\\/?当文档另有说明时为什么需要它?
我正在使用通配符server_name.我想将example.com(配置为*.example.com)的所有子域重定向到foo.com除外xyz.example.com
我有如下配置
server {
listen 80;
server_name *.example.com;
location / {
proxy_pass http://$1.foo.com;
}
}
Run Code Online (Sandbox Code Playgroud)
我不想改变任何请求 xyz.example.com
我正在寻找nginx配置来设置docker存储库
###########################################################
## this configuration was generated by JFrog Artifactory ##
###########################################################
## add ssl entries when https has been set in config
ssl_certificate /etc/nginx/ssl/demo.pem;
ssl_certificate_key /etc/nginx/ssl/demo.key;
ssl_session_cache shared:SSL:1m;
ssl_prefer_server_ciphers on;
## server configuration
server {
listen 443 ssl;
listen 80 ;
server_name ~(?<repo>.+)\.art.local art.local;
if ($http_x_forwarded_proto = '') {
set $http_x_forwarded_proto $scheme;
}
## Application specific logs
## access_log /var/log/nginx/art.local-access.log timing;
## error_log /var/log/nginx/art.local-error.log;
rewrite ^/$ /artifactory/webapp/ redirect;
rewrite ^/artifactory/?(/webapp)?$ /artifactory/webapp/ redirect;
rewrite ^/(v1|v2)/(.*) /artifactory/api/docker/$repo/$1/$2;
chunked_transfer_encoding on;
client_max_body_size 0; …Run Code Online (Sandbox Code Playgroud) 我的Nginx配置有问题.我有2台服务器,一台使用nginx,另一台使用我的webApp在symfony3中.这是我的配置:
location /portal/mysite/ {
set $frontRoot /srv/data/apps/mysite-portal-stag/current/web;
set $sfApp app.php; # Change to app.php for prod or app_dev.php for dev
root /srv/data/apps/mysite-portal-stag/current/web;
rewrite ^/portal/mysite/(.*)$ /$1 break;
try_files $uri @sfFront;
}
location @sfFront {
root /srv/data/apps/mysite-portal-stag/current/web;
fastcgi_pass myserver:myport;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $frontRoot/$sfApp;
fastcgi_param SCRIPT_NAME /portal/mysite/$sfApp;
}
Run Code Online (Sandbox Code Playgroud)
webSite适用于所有php脚本,但所有资产(静态文件)都是损坏的文件.我不太了解Nginx如何工作来指示什么是静态文件并"告诉"我的代理他们不是脚本.
nginx ×10
nginx-location ×10
nginx-config ×2
regex ×2
regex-group ×2
angular ×1
artifactory ×1
docker ×1
fastcgi ×1
jenkins ×1
symfony ×1