簡體   English   中英

nginx-基於位置的代理/重寫

[英]nginx - proxy/rewrite based on location

我試圖將所有以/ api /開頭的請求重定向到localhost上的節點服務器。 我一直無法讓nginx正確重寫請求。

我的server.conf(我包括了整個文件,以防萬一我不注意有沖突):

server {
    listen 80;

    root /var/www/sites/my.server;
    index index.php index.html index.htm;

    server_name .my.server;
    access_log /var/log/nginx/my.server-access.log;
    error_log /var/log/nginx/my.server-error.log;

    location / {
            try_files $uri $uri/ /index.html;
    }

    ## Redirect api to node server
    location /api {
            rewrite ^/api/(.*)$ /$1 last;
            proxy_pass      http://127.0.0.1:3030/;
            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;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
          root /usr/share/nginx/www;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

    if (!-e $request_filename){
        rewrite ^(.*)$ /index.php?q=$1 last;
        break;
    }

    # SSL Related Setup
    listen 443 ssl;
    ssl on;
    ssl_certificate /etc/ssl/certs/ssl-bundle.crt;
    ssl_certificate_key /etc/ssl/private/my.server.key;

    #enables all versions of TLS, but not SSLv2 or 3 which are weak and now deprecated.
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    #Disables all weak ciphers
    ssl_ciphers RC4:HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
}

使用此配置, http://my.server/api被正確重定向到節點服務器,而http://my.server/api/jobs沒有被重定向。

經過反復試驗和搜索,我發現以下作品:

location ^~ /api/ {
        rewrite ^/api/(.*) /$1 break;
        proxy_pass        http://127.0.0.1:3030/;
        proxy_set_header  Host            $host;
        proxy_set_header  X-Real-IP       $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM