On NGINX I am trying to figure out how to keep nice URL in address bar after redirecting to the php file. I have tried all the mentioned below in the code and it works fine but no matter if use redirect or proxy, I still see www.mydomain.com/products.php in the address bar. And I need to keep the address looking as www.mydomain.com/products. Any ideas please?
server {
server_name mydomain.com www.mydomain.com;
root /var/www/mydomain.com;
index index.html index.php;
location / {
try_files $uri $uri/ =404;
}
location /products {
#rewrite ^/products$ /products.php last;
#try_files $uri $uri/ /products.php;
proxy_pass http://localhost/products.php;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Resolved, I overcomplicated that, the simplest way works fine.
location /products {
rewrite ^/products /products.php last;
}
Related
While checking the GCP error log i got a issue (rewrite or internal redirection cycle).
I cannot figure out why this error is happening: "146039 rewrite or internal redirection cycle while internally redirecting to "/static/js/main.64520b4a.js" in nginx.conf
Below is my Config file:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_tokens off;
location / {
if (-f $document_root/$maintenance_mode) {
rewrite ^(.*)$ /maintenance_page.html break;
}
try_files $uri /index.html =404;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Request-URI $request_uri;
}
location ~ .(static)/(js|css|media)/(.+)$ {
try_files $uri $uri/ /$1/$2/$3;
}
}
guys if you know the solution, kindly share
You're try_files statement is redirecting to /$1/$2/$3 which causes the continuous loop.
If the /$1/$2/$3 is intended to be a file term (see this document) - then it cannot be the final parameter.
For example, you could append =404 to create a valid try_files statement:
try_files $uri $uri/ /$1/$2/$3 =404;
I am trying to host two react applications using nginx, everyting works expect query params ?test=1
location / {
alias /root/phone_build/;
index index.html;
try_files $uri /index.html$is_args$args =404;
}
location /web_build {
alias /root/web_build/;
index index.html;
try_files $uri /index.html$is_args$args =404;
}
#location / {
location /api {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_connect_timeout 300s;
proxy_cache_bypass $http_upgrade;
}
I tried with ?$query_string but still doesn't work. Any recommendations? Thank your
You are using try_files incorrectly. The file terms match filenames and do not need the $is_args$args, otherwise, when you add a query string to the request, you will simply force a 404 response.
Also, use root instead of alias. The alias directive is only required for special cases.
Try:
index index.html;
location / {
root /root/phone_build;
try_files $uri /index.html =404;
}
location /web_build {
root /root;
try_files $uri /web_build/index.html =404;
}
location /api {
...
}
I have the following block in my nginx configuration file:
location /cat {
try_files $uri $uri/ /index.php?url=$uri;
}
When I try any of the following URLs, this works as expected:
http://example.com/cat/test
http://example.com/cat/test/test
http://example.com/cat/t
The following, however, do not. Instead of a redirect, I get a 404 error.
http://example.com/cat/test/
http://example.com/cat/test/test/
http://example.com/cat/t/
I'm not very experienced with nginx, so let me know what further information might be needed. This has me quite stumped.
Edit: I did come across this, but I would prefer to avoid using rewrite (for the sake of efficiency), plus this seems like an unnecessary band-aid:
https://serverfault.com/questions/755646/nginx-use-try-files-to-serve-file-instead-of-directory
Edit 2: Here is the full config
server {
listen 64.40.99.86:80;
server_name example.com;
server_name www.example.com;
client_max_body_size 128m;
root "/var/www/vhosts/example.com/httpdocs";
access_log "/var/www/vhosts/system/example.com/logs/proxy_access_log";
error_log "/var/www/vhosts/system/example.com/logs/proxy_error_log";
if ($host ~* ^example.com$) {
rewrite ^(.*)$ http://www.example.com$1 permanent;
}
location / {
proxy_pass http://64.40.99.86:7080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
location /internal-nginx-static-location/ {
alias /var/www/vhosts/example.com/httpdocs/;
add_header X-Powered-By PleskLin;
internal;
}
location ~ ^/~(.+?)(/.*?\.php)(/.*)?$ {
alias /var/www/vhosts/example.com/web_users/$1/$2;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass "unix:///var/www/vhosts/system/example.com/php-fpm.sock";
include /etc/nginx/fastcgi.conf;
}
location ~ ^/~(.+?)(/.*)?$ {
proxy_pass http://64.40.99.86:7080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
location ~ \.php(/.*)?$ {
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass "unix:///var/www/vhosts/system/example.com/php-fpm.sock";
include /etc/nginx/fastcgi.conf;
}
location ~ /$ {
index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;
}
location /cat {
try_files $uri $uri/ /index.php?url=$uri;
}
}
I want to understand the connections between Nginx Config Files, Domains and Wordpress Site URL.
I map my domain blog.example.com to my ip: xxx.xxx.xxx.xxx/wordpress/
in the Wordpress SiteUrl i set blog.example.com
How should my NGINX configuration file look like?
Edit
My Configuration works in the first step, I get to the Frontpage, but when clicking on permalinks, I get redirected to the Frontpage again instead of the Post.
My current Config looks like this:
server {
listen 80 default_server;
root /var/www/wordpress/;
index index.html index.htm index.php
server_name blog.example.com;
location ~\.php$ {
try_files $uri =404;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param REQUEST_URI $uri?$args;
}
location / {
try_files $uri /index.php$args;
}
}
Edit
In Nginx Debug Log I see the REQUEST_URI is /wordpress/postname/
but as i understand it should be just /postname/
How about this:
server {
listen 80;
server_name _;
return 404; # default
}
server {
listen 80;
server_name blog.example.com;
location / {
return 301 http://blog.example.com/wordpress;
}
location ^~ /wordpress {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://xxx.xxx.xxx.xxx;
}
}
I'm creating a nginx reverse proxy to apache. Apache runs on port 8080 and nginx on 80.
I hope to achieve the following;
When I request the page http://server/test.html it should be proxyed to http://server:8080/unknown.html
Later on I'll do some eval stuff on the pages and redirect the users to the right pages, but I can't even get this to work. I get the test.html back as response all the time.
My nginx config:
server {
listen 80;
root /var/www/;
index index.php index.html index.htm;
server_name example.com;
location / {
# try_files $uri $uri/ /index.php;
}
location ~ \.html$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
#rewrite ^/unknown.html;
proxy_pass http://127.0.0.1:8080;
proxy_redirect http://127.0.0.1/test.html http://127.0.0.1:8080/unknown.html;
}
location ~ /\.ht {
deny all;
}
}
I have zero experience yet, but I'm eager to learn how to get this working...
Firstly, the proxy_redirect directive is the opposite of what you need. It is only used to rewrite the Location response headers in 3xx responses from upstream. See this document for details.
You can use a rewrite ... break statement within the location block that performs the proxy_pass, for example:
location ... {
rewrite ^/test.html$ /unknown.html break;
proxy_pass ...;
}
See this document for details.