Configurare la riscrittura dell'URL per un bilanciatore del carico delle applicazioni classico
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questo esempio mostra la riscrittura del percorso specificato in una richiesta per un bilanciatore del carico delle applicazioni classico.
Per configurare la gestione del traffico per i bilanciatori del carico delle applicazioni esterni globali e per i bilanciatori del carico delle applicazioni esterni regionali, consulta le seguenti pagine:
Nella colonna a sinistra dello schermo, fai clic su Regole host e percorso.
Seleziona Regola host e percorso avanzata (reindirizzamento URL, riscrittura URL).
Fai clic sulla riga contenente la regola del percorso non predefinita, in questo caso la riga con un asterisco (*) per tutti gli host.
Fai clic sull'icona a forma di matita edit per la riga /love-to-fetch/* Route traffic to a single backend: dogs.
In Percorsi, elimina /love-to-fetch/* e aggiungi /*.
In Azione, seleziona Instrada il traffico a un singolo backend.
Fai clic su Azione aggiuntiva (riscrittura URL).
Lascia vuoto il campo Riscrittura host.
In Riscrittura prefisso percorso, inserisci /love-to-fetch/.
In Backend, seleziona dogs.
Fai clic su Salva.
Fai clic su Fine.
La nuova regola host e percorso è la seguente:
Percorsi
Azione
Backend
Qualsiasi senza corrispondenza (valore predefinito)
Instrada il traffico a un singolo backend
gatti
/*
Instrada il traffico a un singolo backend
cani
Se è tutto corretto, fai clic su Aggiorna per aggiornare il bilanciatore del carico HTTP.
gcloud
Crea un file YAML /tmp/http-lb.yaml, assicurandoti di sostituire
PROJECT_ID con il tuo ID progetto.
Quando un utente richiede il percorso /*, il percorso viene riscritto nel backend in base alla posizione effettiva dei contenuti, ovvero /love-to-fetch/*.
defaultService:https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/catshostRules:-hosts:-'*'pathMatcher:path-matcher-1name:http-lbpathMatchers:-defaultService:https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/catsname:path-matcher-1pathRules:-paths:-/*routeAction:urlRewrite:pathPrefixRewrite:/love-to-fetch/service:https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/dogstests:-description:Test routing to backend bucket, dogshost:example.compath:/love-to-fetch/testservice:https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/dogs
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-08 UTC."],[],[],null,["# Set up URL rewrite for a classic Application Load Balancer\n\nThis example demonstrates rewriting the path specified in a request for\na classic Application Load Balancer.\n\nTo configure traffic management for global external Application Load Balancers and\nregional external Application Load Balancers, see the following pages:\n\n- [Setting up traffic management for\n global external Application Load Balancer](/load-balancing/docs/https/setting-up-global-traffic-mgmt#rewrite-url)\n- [Setting up traffic management for\n regional external Application Load Balancer](/load-balancing/docs/https/setting-up-reg-traffic-mgmt#rewrite-url)\n\n### Before you begin\n\n- Read about [URL rewrites](/load-balancing/docs/https/traffic-management#rewrites).\n\n- Read about [URL maps in general](/load-balancing/docs/url-map-concepts) and\n [path matchers](/load-balancing/docs/url-map-concepts#pm-constraints)\n in particular.\n\n- The URL rewrite example assumes that you've already created your\n external Application Load Balancer following the steps outlined in [Setting up a load balancer\n with Cloud Storage\n buckets](/load-balancing/docs/https/ext-load-balancer-backend-buckets).\n\n That example leads you through the creation of two resources:\n - `http://`\u003cvar translate=\"no\"\u003eIP_ADDRESS\u003c/var\u003e`/never-fetch/three-cats.jpg`\n - `http://`\u003cvar translate=\"no\"\u003eIP_ADDRESS\u003c/var\u003e`/love-to-fetch/two-dogs.jpg`\n\n Where `/never-fetch/three-cats.jpg` is stored in `/cats`, which is\n the default service, and `/love-to-fetch/two-dogs.jpg` is stored in\n `/dogs`.\n\n At this point, the URL map looks like this: \n\n ```\n gcloud compute url-maps describe http-lb\n ``` \n\n creationTimestamp: '2020-10-13T11:18:10.561-07:00'\n defaultService: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/global/backendBuckets/cats\n fingerprint: MKfYsObzqgw=\n hostRules:\n - hosts:\n - '*'\n pathMatcher: path-matcher-1\n id: '1420501688756228493'\n kind: compute#urlMap\n name: test-bucket\n pathMatchers:\n - defaultService: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/global/backendBuckets/cats\n name: path-matcher-1\n pathRules:\n - paths:\n - /love-to-fetch/*\n service: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/global/backendBuckets/dogs\n selfLink: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/global/urlMaps/http-lb\n\n### Modifying the URL map\n\n| **Important:** The rewrite is prepended to the path as is. Full path rewrites are not supported. External Application Load Balancers only implement path prefix rewrites. For example, you *can* rewrite: `host.name/path1/resource1` to `host.name/path2/resource1`. You *cannot* rewrite `host.name/path1/resource1` to `host.name/path1/resource2`.\n\nIn this example, you rewrite the `/love-to-fetch/` URL so that users can reach the\n`two-dogs.jpg` image with this simplified URL:\n\n- `http://`\u003cvar translate=\"no\"\u003eIP_ADDRESS\u003c/var\u003e`/two-dogs.jpg`\n\nTo do this, modify the load balancer's URL map as follows: \n\n### Console\n\n### Edit your load balancer\n\n1. Go to the Load balancing page in the Google Cloud console. \n [Go to the Load balancing page](https://console.cloud.google.com/networking/loadbalancing/add)\n2. Click `http-lb`.\n3. Click **Edit** edit.\n4. Keep the window open to continue.\n\n### Change the host and path rules\n\n1. In the left column of the screen, click **Host and path rules**.\n2. Select **Advanced host and path rule (URL redirect, URL rewrite)**.\n3. Click the row that contains the non-default path rule, in this case, the row that has an asterisk (`*`) for all hosts.\n4. Click the pencil icon edit for the `/love-to-fetch/* Route traffic to a single backend: dogs` row.\n5. Under **Paths** , delete `/love-to-fetch/*` and add `/*`.\n6. Under **Action** , select **Route traffic to a single backend**.\n7. Click **Add-on action (URL rewrite)**.\n8. Leave **Host rewrite** blank.\n9. Under **Path prefix rewrite** , enter `/love-to-fetch/`.\n10. Under **Backend** , select `dogs`.\n11. Click **Save**.\n12. Click **Done**.\n The new host and path rule looks as follows:\n\n \u003cbr /\u003e\n\n13. If everything looks correct, click **Update** to update your HTTP\n load balancer.\n\n### gcloud\n\n1. Create a YAML file `/tmp/http-lb.yaml`, making sure to substitute\n \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with your project ID.\n\n When a user requests path `/*`, the path gets rewritten in the backend to\n the actual location of the content, which is `/love-to-fetch/*`. \n\n defaultService: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/global/backendBuckets/cats\n hostRules:\n - hosts:\n - '*'\n pathMatcher: path-matcher-1\n name: http-lb\n pathMatchers:\n - defaultService: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/global/backendBuckets/cats\n name: path-matcher-1\n pathRules:\n - paths:\n - /*\n routeAction:\n urlRewrite:\n pathPrefixRewrite: /love-to-fetch/\n service: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/global/backendBuckets/dogs\n tests:\n - description: Test routing to backend bucket, dogs\n host: example.com\n path: /love-to-fetch/test\n service: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/global/backendBuckets/dogs\n\n2. Validate the URL map.\n\n ```\n gcloud compute url-maps validate --source /tmp/http-lb.yaml\n ```\n\n If the tests pass and the command outputs a success message,\n save the changes to the URL map.\n3. Update the URL map.\n\n ```\n gcloud compute url-maps import http-lb \\\n --source /tmp/http-lb.yaml \\\n --global\n ```\n\n### Testing\n\nNote the IPv4 address that was reserved: \n\n```\ngcloud compute addresses describe example-ip \\\n --format=\"get(address)\" \\\n --global\n```\n\nAfter waiting several minutes for the changes to propagate, you can test this\nsetup.\n\nOn the command line, run the following curl command: \n\n```\ncurl http://IP_ADDRESS/two-dogs.jpg\n```\n\nIn a browser, open `http://`\u003cvar translate=\"no\"\u003eIP_ADDRESS\u003c/var\u003e`/two-dogs.jpg`.\n\nWhat's next\n-----------\n\n- [Clean up the load balancer setup](/load-balancing/docs/cleaning-up-lb-setup)."]]