Im using an nginx proxy, and am trying to replace the base url with my server url. For example I would like to proxy www.google.com through my localhost:8000, and replace all base url instances (www.google.com) with (localhost:8000).
My nginx.conf so far:
server {
listen 8080;
server_name localhost;
location / {
proxy_pass http://www.google.com/;
rewrite ^/.google.com /localhost:8080 last;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Just remove rewrite directive in 'location /' block. And add break;
location / {
proxy_pass http://www.google.com/;
break;
}
Related
by setting a default.conf as follows:
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /some-path {
proxy_pass http://<SERVER_IP>:8050;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
I'm able to make sure that the url http:/URL/some-path displays what is present at http:/URL:8050/some-path .
Which is the syntax to obtain that http:/URL/some-path displays what is present at http:/URL:8050 (i.e. without need to specify some-path in the second URL)?
Is it possible to configure nginx reverse proxy where http://localhost/a/ leads to 1 site and http://localhost/b/ to another? I tried this config:
server {
listen 80;
server_name localhost;
location /a/ {
proxy_pass http://apache.org/;
}
location /b/ {
proxy_pass http://www.gnu.org/;
}
it almost works, but all the links returned by web pages are missing /a/ or /b/ prefix so it can't load any pictures, styles and etc. For example link http://localhost/css/styles.css is not working, but http://localhost/a/css/styles.css is working.
Is there a directive that will append all links on page with suitable prefix? Or there is different approach to put a websites on separate locations?
#ivan-shatsky, Thank you very much.
Just wanted to add working config if some1 else needs.
map $http_referer $prefix {
~https?://[^/]+/a/ a;
default b;
}
server {
listen 80;
server_name localhost;
location / {
try_files /dev/null #$prefix;
}
location /a/ {
proxy_pass http://apache.org/;
}
location #a {
proxy_pass http://apache.org;
}
location /b/ {
proxy_pass http://www.gnu.org/;
}
location #b {
proxy_pass http://www.gnu.org;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}```
I'm currently working on a JS Project, that uses the url path. Now if I go on my website with example.com/, the JavaScript won't work, because I actually need example.com/index.html.
I'm already using an reverse proxy to proxy pass to two different docker containers. So my idea was to pass the request to example.com/index.html when example.com/ is called. But I can't figure out the regex stuff to achieve this goal.
My old config:
server {
listen 80;
server_name example.com;
# allow large uploads of files - refer to nginx documentation
client_max_body_size 1G;
# optimize downloading files larger than 1G - refer to nginx doc
before adjusting
#proxy_max_temp_file_size 2G;
location / {
proxy_pass http://structure.example:80;
}
location /cdn {
proxy_pass http://content.example:80;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Stuff I tried:
server {
listen 80;
server_name example.com;
# allow large uploads of files - refer to nginx documentation
client_max_body_size 1G;
# optimize downloading files larger than 1G - refer to nginx doc
before adjusting
#proxy_max_temp_file_size 2G;
location / {
proxy_pass http://structure.nocms:80/index.html;
}
location ~* \S+ {
proxy_pass http://structure.nocms:80;
}
location /cdn {
proxy_pass http://content.nocms:80;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
The accepted answer has one disadvantage: going to example.com explicitly redirects to example.com/index.html (that is, returns 301 Moved permanently), which is not always desired.
Instead, I suggest to prepend location / with another directive, location = /, which is designed to the root URL only:
location = / {
proxy_pass http://structure.nocms:80/index.html;
}
location / {
proxy_pass http://structure.nocms:80;
}
The above instructs nginx to pass requests to example.com directly to http://structure.nocms:80/index.html, while requesting any other URLs in example.com/* would pass the request to the corresponding URL in the downstream.
Below config should work for you
server {
listen 80;
server_name example.com;
# allow large uploads of files - refer to nginx documentation
client_max_body_size 1G;
# optimize downloading files larger than 1G - refer to nginx doc
before adjusting
#proxy_max_temp_file_size 2G;
location = / {
rewrite ^ /index.html permanent;
}
location / {
proxy_pass http://structure.example:80;
}
location /cdn {
proxy_pass http://content.example:80;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
I would like to do like this
localhost/ | index.html
localhost/category | index.html
localhost/notes | index.html
localhost/notes/note1 | index.html
And I tried to do this but infinity mistakes stoped me.
When I'm trying to use localhost then I see it.
Rewrite or internal redirection cycle while processing "/index.html" - this error 500.
server {
listen 80;
server_name localhost;
root C:/Users/Sergey/Desktop/xxx/angular2do;
location / {
if ($request_method = POST) {
proxy_pass http://localhost:3000;
}
if ($request_method = GET) {
rewrite ^.*$ /index.html last;
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# root html;
# }
}
I know stack has a lot of questions for this problem but I used to be all of them and always catched error.
Using rewrite ... last inside a location block will just cycle around rewriting /index.html to /index.html. You should use rewrite ... break instead. See this document for details.
If your application also requires resources (css, js, images), you may adopt an alternative approach that returns the static file if it exists, for example:
server {
listen 80;
root C:/Users/Sergey/Desktop/xxx/angular2do;
location / {
try_files $uri #other;
}
location #other {
if ($request_method != POST) { rewrite ^ /index.html last; }
proxy_pass http://localhost:3000;
}
}
I have the following vhost entry
server {
listen 80;
server_name example.com www.example.com;
#access_log /var/log/nginx/nginx-access.log;
location /media/ {
root /home/luke/django/solentcms;
}
location /admin/media/ {
root /home/luke/virts/django1.25/lib/python2.7/site-packages/django/contrib/admin/media;
}
location / {
proxy_pass http://127.0.0.1:8001;
}
error_page 404 /404.html;
location = /404.html {
root /home/luke/django/solentcms/404;
allow all;
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/luke/django/solentcms/404;
allow all;
}
}
However, 404's and 50x errors are still be re-directed to the horrible nginx default pages.Any ideas as to why? This syntax works on one of my other servers.
Cheers.
Are the errors coming from your backend? You may need to add proxy_intercept_errors on; alongside your proxy_pass.