uri 빼기 위치를 얻는 Nginx 정규식

uri 빼기 위치를 얻는 Nginx 정규식

몇 가지 응용 프로그램에 대한 역방향 프록시로 Nginx를 실행하고 있습니다. 하나의 위치 지시문이 올바르게 실행되어 unix 소켓 파일과 해당 업스트림 wsgi 앱으로 요청을 보냅니다. 내가 문제가있는 지시어는location ~ ^/sub/alarm(.*)$. 작동하는 것처럼 보이는 몇 가지 재작성이 있지만 다른 의도와 충돌하는 경우 각 지시문에 대해 내 의도를 설명하겠습니다.

  • 첫 번째 서버 지시문은 모든 http를 https로 리디렉션해야 합니다. 이것은 잘 작동하는 것 같습니다.
  • 두 번째 서버 지시문에는 wsgi 애플리케이션으로 트래픽을 전달하는 하나의 위치 지시문이 있습니다. 이것은 잘 작동합니다. 다른 위치 지시문/home/myuser/alarm.example.com/GET이 수신될 때 부터 정적 콘텐츠를 제공하는 데 사용하려고 했습니다 .example.net/sub/alarm. (예: example.net/sub/alarm/pretty.css는 /home/myuser/alarm.example.com/pretty.css를 넘겨야 합니다.) 대신 wsgi 앱이 로드됩니다.
  • 와일드카드 인증서는 없지만 쉬운 바로가기와 암호화를 원했기 때문에 마지막 서버 지시어는 Alarm.example.net을 example.net/sub/alarm으로 리디렉션해야 합니다. 이것은 잘 작동하는 것 같습니다.

구성:

server {
    listen 80;
    listen [::]:80 ipv6only=on;
    server_name example.com www.example.com;
    rewrite ^/(.*) https://example.com/$1 permanent;
}

server {
        listen 443 ssl;
        listen [::]:443 ipv6only=on ssl;
        charset utf-8;
        client_max_body_size 75M;
        server_name example.com www.example.com;
        ssl_certificate /etc/ssl/certs/example.com.crt;
        ssl_certificate_key /etc/ssl/private/example.com.key;

        location / {
                include uwsgi_params;
                uwsgi_pass unix:///tmp/example.com.sock;
        }

        location ~ ^/sub/alarm(.*)$ {
                alias /home/appusername/alarm.example.com;
                index index.html;
                try_files $1 $1/;
        }
}

server {
        listen 80;
        server_name alarm.example.com;
        rewrite ^ $scheme://example.com/sub/alarm$request_uri permanent;
}

나는 보았다nginx 구성의 $uri에서 위치 블록을 제거하는 방법은 무엇입니까?URI 다음에 내 위치 파일의 일부를 가져오려고 합니다. 우선순위에 관해 제가 뭔가를 놓치고 있는 것 같아요.

또 다른 시도정규 표현식이 없었습니다.

location /sub/alarm/ {
        alias /home/appusername/alarm.example.com;
        index index.html;
        try_files $uri $uri/index.html =404;
}

위의 경우 Alarm.example.com으로 이동할 때 index.html을 로드할 수 있었습니다(올바로 리디렉션됨).https://example.com/sub/alarm/), 그러나 모든 리소스에서 404가 발생했습니다.

마지막으로 두 가지 시도를 결합하려고 시도했지만 위치 블록 안에 물결표를 넣을 수 없는 것 같습니다(Nginx를 다시 로드할 때 '알 수 없는 지시문').

location /sub/alarm/ {
        ~ ^/sub/alarm(.)$
        try_files /home/appusername/alarm.example.com$1 /home/appusername/alarm.example.com$1/;
}

추가 참고 사항

  • example.com의 동적 앱은 정적인 "알람" 앱과 전혀 관련이 없습니다. 정규식을 시도할 때 알람 앱 대신 제공되기 때문에 포함되었습니다.
  • 나는 항상 정규식에 대해 아무것도 배우지 않았으며(아마도 현명하지 못할 수도 있지만 지난 7년 동안 오늘까지 실제로 필요하지 않았습니다) Nginx를 구성하고 있으므로 대가를 지불하고 있습니다. 나는 사용했다정규식 101내 정규식 문자열을 얻으려면 ^\/sub\/alarm(.*)$. 이스케이프 슬래시를 사용해야 한다는 것을 나타내는 것처럼 보였지만 Nginx는 예제에서 이를 표시하지 않는 것 같습니다. 또 공부해야 할 개념이 있으면 알려주세요. 오늘부터 공식적으로 정규식 회피 입장을 종료합니다.
  • Nginx를 다시 로드할 수 있을 만큼 구문이 유효하다면 2015/10/12 20:25:57 [notice] 30500#0: signal process started모든 시도에서 내 오류가 발생한 것입니다.

답변1

그래서 사용자(어... 나)는 눈에 띄는 단서가 되어야 할 것을 놓쳤습니다.

위의 경우 Alarm.example.com으로 이동할 때 index.html을 로드할 수 있었습니다(올바로 리디렉션됨). https://example.com/sub/alarm/), 그러나 모든 리소스에서 404가 발생했습니다.

그 예는 아직 정확하지 않지만,리소스 파일에 대한 파일 권한을 확인했어야 합니다.. Nginx는 www-data로 실행되고 index.html 파일의 그룹에서 실행되지만 모든 파일의 그룹에 있어야 합니다. 파일 소유자는 appusername 사용자입니다.

이후 알람처럼 라우팅되는 또 다른 앱('맥주'라고 함)을 추가했습니다. 이제 정규식의 기본 사항을 배웠으며 한 위치 블록에서 이 작업을 수행할 수 있었습니다.

server {
    listen 80;
    listen [::]:80 ipv6only=on;
    server_name example.com www.example.com;
    rewrite ^/(.*) https://example.com/$1 permanent;
}

server {
        listen 443 ssl;
        listen [::]:443 ipv6only=on ssl;
        charset utf-8;
        client_max_body_size 75M;
        server_name example.com www.example.com;
        ssl_certificate /etc/ssl/certs/example.com.crt;
        ssl_certificate_key /etc/ssl/private/example.com.key;
        error_log /var/log/nginx/error.log warn;

        location ~ ^/sub/(alarm|beer)(.*)$ {
                alias /home/appusername/$1.example.com/;
                #index  index.html index.htm;
                try_files $2 $2/ =404;
        }

        location / {
                include uwsgi_params;
                uwsgi_pass unix:///tmp/example.com.sock;
        }
}

server {
        listen 80;
        server_name alarm.example.com;
        rewrite ^ $scheme://example.com/sub/alarm$request_uri permanent;
}

위치 블록의 순서에 따라 스위치를 신경 쓰지 마십시오. 그것은 단지 타이핑과 재타이핑의 우연일 뿐입니다.

관련 정보