I am using using nginx and wants to change my url from page.php to page.html
there are two things I want to achieve,
Change url from domain.com/page.php --> domain.com/page.html
The anchore tag in my page is domain.com/page.html but the actual page is page.php. I want this to work without changing my anchor tag.
I have a url like domain.com/page.php?get=value this must be like domain.com/value
Due to limited knowledge in nginx please suggest me the script
When someone states that they want to rewrite a URL from A to B, they often mean the other way around. What they really want is for the URL address bar to show B, but actually access an existing internal resource at A.
From your question, I think that you want the URL address bar to show page.html but internally the page.php resource is served.
Let's assume that you have mixed content, some .html and some .php, so first you might want to remove any .html extension with an internal rewrite so that both .html and .php filenames can be tested. An internal rewrite is one that will not affect the URL address bar, but just makes it easier for the server to internally route the request.
location ~* \.(html|php)$ {
rewrite ^(.*)\.(html|php)$ $1 last;
}
The root location can then process extension-less URIs and test for the presence of .html and .php files, and anything else you fancy.
location / {
try_files $uri $uri/ $uri.html #php;
}
The PHP files are offloaded to a named location which contains the code to send the request upstream to the PHP interpreter:
location #php {
try_files $uri.php /page.php?get=$uri;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass ...;
}
All of the above nginx directives are documented here.
Related
My URL is: https://example.com/data=123456
I added the following line of code to the index.php file: var_dump($_GET['data']);
But this only works if I add a ? character to the URL.
(example: https://example.com/?data=123456)
This is in the root of index.php.
I tried to add this to the nginx.conf file:
location / {
try_files $uri $uri/ /index.php$is_args$args;
rewrite ^(.*)\?(.*)$ $1$2 last;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
rewrite ^(.*)\?(.*)$ $1$2 last;
include fastcgi.conf;
}
But not working. How to remove the "?" character from URL?
When your URL is https://example.com/data=123456, the data=123456 is a part of URL path. When your URL is https://example.com/?data=123456, the data=123456 is a query part of the URL, where query argument data has a value if 123456. Check at least the Wikipedia article.
Next, only query arguments can be accesses via $_GET array. Full request URI (including both path and query parts) is available via $_SERVER['REQUEST_URI']. You can analyze that variable in your PHP script and skip the rewrite part completely.
If you want to rewrite your URI and made that data to be accessible via $_GET['data'] variable, it is also possible. However your main mistake is that the rewrite directive works only with (normalized) path of the given URL and not the query part of it. Nevertheless it can be used to append (or replace) query arguments. To do it you can use
rewrite ^(.*/)(data=[^/]*)$ $1?$2;
(to rewrite only data parameter) or more generic
rewrite ^(.*/)([^/=]+=[^/]*)$ $1?$2;
(to count on every param=value form of the URL path suffix). You can place this rewrite rule at the server configuration level.
I have implemented a php application in codeigniter and now want to deploy it to the nginx server. Before deploying I checked my nignx configuration on my localhost using MAMP server. It is working correctly. But, this configuration is not working on the live server. As a beginner in nginx, I am not understanding where is the mistake here. In live server, I can not write in the main nginx.conf file. I have a separate configuration file like "abc" for my application "abc". And all my application files are under "abc/xyz" directory. Here is my sample confuguration,
location /abc {
root /srv/www/htdocs/apps/;
index index.html index.htm index.php;
location /xyz {
try_files $uri $uri/ /abc/xyz/index.php;
}
location ~ \.php(\/(\w+))*$ {
try_files $uri =404;
rewrite (.+)\.php(\/(\w+))*$ $1.php break;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Here, I can see my welcome page https://myapplication/abc/xyz. But if I want to navigate other pages like https://myapplication/abc/xyz/other_pages, it is showing "404 Page not found". I have checked the other solutions but none of them is not working in this case. Thanks in advance for the help!
The location /xyz block is nested within the location /abc block. The nested block is required to precess URIs with a prefix of /abc/xyz.
If there are other regular expression location blocks surrounding your location /abc block, you should use the^~` modifier.
For example:
location ^~ /abc {
...
location /abc/xyz {
...
}
...
}
See this document for more.
Sorry for the late answer. It was actually very silly mistake. My controller page name was in small character. This is why it was not working. My configuration is okay. The first letter of the controller page should be in capital character. For example, my controller name is Home. So my php file name must be Home.php not home.php.
I have a problem with my Nginx configuration. I have 2 servers, one with nginx and one with my webApp in symfony3.
Here is my configuration :
location /portal/mysite/ {
set $frontRoot /srv/data/apps/mysite-portal-stag/current/web;
set $sfApp app.php; # Change to app.php for prod or app_dev.php for dev
root /srv/data/apps/mysite-portal-stag/current/web;
rewrite ^/portal/mysite/(.*)$ /$1 break;
try_files $uri #sfFront;
}
location #sfFront {
root /srv/data/apps/mysite-portal-stag/current/web;
fastcgi_pass myserver:myport;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $frontRoot/$sfApp;
fastcgi_param SCRIPT_NAME /portal/mysite/$sfApp;
}
The webSite work for all the php scripts but all the assets (static files) are broken files. I don't understand enough how Nginx works to indicate what are the static files and "tell" my proxy that they aren't script.
The try_files directive automatically tries to find static files, and serve them as static, prior to giving up, and letting the request be served as a script.
http://nginx.org/r/try_files
Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context. The path to a file is constructed from the file parameter according to the root and alias directives. It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to the uri specified in the last parameter is made.
Note that although you're already using try_files, it appears that perhaps your path handling isn't up to spec.
As for your own answer with a temporary solution, there's nothing wrong with using a rewrite or two, but that said, it looks like you'd benefit from the alias directive.
http://nginx.org/r/alias
Defines a replacement for the specified location.
However, you've never explained why you're serving stuff out of /tmp. Note that /tmp is often automatically cleared by some cron scripts, e.g., on OpenBSD, the /etc/daily script would automatically find and remove files older than about 7 days (on a daily basis, as the name suggests).
In summary, you should first figure out what is the appropriate mapping between the web view of the filesystem and your filesystem.
Subsequently, if a prefix is found, just use a separate location for the assets, together with alias.
Else, figure out the paths for try_files to work as intended.
I have find a very ugly solution until anyone find a better solution, here is what I have done :
I have copied all the assets repository and copied it to my proxy server where nginx is.
Here is my new config :
location /portal/mysite/ {
set $frontRoot /srv/data/apps/mysite-portal-stag/current/web;
set $sfApp app.php;
root /srv/data/apps/mysite-portal-stag/current/web;
rewrite ^/portal/mysite/(.*)$ /$1 break;
try_files $uri #sfFront;
}
location /portal/mysite/asset {
root /tmp/mysite/asset;
rewrite ^/portal/mysite/asset/(.*)$ /$1 break;
}
location #sfFront {
set $frontRootWeb /srv/data/apps/mysite-portal-stag/current/web;
root /srv/data/apps/mysite-portal-stag/current/web;
fastcgi_pass myAdressWeb:myPort;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $frontRoot/$sfApp;
fastcgi_param SCRIPT_NAME /portal/mysite/$sfApp;
}
And now it's working, all the js/css and pictures are found.
If anyone think about a "cleaner" answer, he is more than welcome to answer.
I have an s3 bucket that contains directories and html files. I'm using nginx in front of the bucket and have configured a proxy_pass to the bucket, which works fine. I want clean URLs without the html extension, and for these URLs to resolve properly. For example, instead of foo.com/bar/my-file-name.html, this must be accessible at foo.com/bar/my-file-name
The primary question here is how to prevent requiring the .html at the end of every URL. As in, I want nginx to handle routing each requests and try looking for a file with the .html extension, and then return it.
My problem is that I can't figure out how to force nginx to try_files like I would on a non-S3 site. When I do try and add any configuration of try_files, it breaks the site by returning HTTP 500 errors on all paths.
For comparison, here is the configuration that works great on a non-S3 backed site (with the files on the server itself):
location / {
try_files $uri $uri/ #htmlext;
}
location #htmlext {
rewrite ^(.*)$ $1.html last;
}
My goal is to replicate this behavior with a proxy_pass to an S3 bucket and force it to try filenames with an .html extension other than index.html, which works out of the box.
Here is my current configuration that works great, minus any html files other than index.html:
location / {
proxy_pass my-bucket.s3.blahblah/;
}
I'm having a big headache while configuring Nginx to work inside a location block.
I'm developing a web application with Laravel, and it is located at /srv/http/zenith. With Laravel, the index is inside the public folder, so I'm trying to reach it using the following configuration:
location /zenith/ {
root /srv/http/zenith/public;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
But it gets me 404 error everytime. As I read from Nginx documentation, Nginx does not remove the path from the URI, so even inside /zenith/ block, all URIs still start with /zenith/. This way, example.com/zenith points to /srv/http/zenith/public/zenith when I want /srv/http/zenith/public.
How do I fix this error? I expected that Nginx removed this unwanted part automatically, but it seems to be not this way.
You need to understand the difference between a root and alias. A root maps the URI / to the directory mentioned and expects all URI parts after it to match the on disk tree. Alias maps the location of the block it's part of to the directory mentioned and expects all URI parts after this location to match the on disk tree. Since root inside a location block still maps the / URI, the part after / needs to exist on disk for things to work. In the common case you will use root for the document root and alias for location blocks.