Hi I am new to nginx and trying to remove folder name from the URL.
Here is how my URL is accessible:
localhost/project_name/frontend/dashboard
localhost/project_name/frontend/feed
I want to remove the folder named 'frontend' from the URL. the folder contains all of my rendering templates. I have used following configuration but I get 404 when I enter the URL in browser after this configuration:
location /project_name/frontend {
try_files $uri $uri/frontend /project_name/ #extensionless-php;
}
I am not sure whether the configuration is correct, I have tried several configurations but none of them helps.
P.S: This is my just first day on nginx, so pardon if I miss something very obvious.
Thank your for your help.
Related
I have a question about my django settings and nginx in order to display a download link receives in an email generated by a Django view.
1- Context
The emailing part works fine. I can receive this one. In this email I created a link which let to download a file stored in MEDIA folder.
My issue is about the url generated in the email which works with localhost, but not on my testing environment.
2- My code in my local environment
In order to build my download link, I pick up the protocol and domain through :
url = self.request.build_absolute_uri(reverse('home'))
Then, in my message, I created a link like this :
Download link to your export file
In local, it gives me :
http://localhost:8000//download_export/<my_file>/
As you can see, I have a double slashes in my generated url which makes an error.
I solved this issue with :
url = request.build_absolute_uri('/').strip("/")
The urls.py file looks like this :
urlpatterns = [
url(r'^home$', HomeView.as_view(), name='home'),
...
url(r'^download_export/(?P<token>.*)/$', ExportDownloadView.as_view(), name='export_download'),
]
3- My code in my testing environment
In this environment, I'm using nginx as webserver. The application is available from : https://subdomain.domain.fr/dev3/<app_name>/home
The nginx.conf looks like :
location /dev3/<app_name> {
include uwsgi_params;
uwsgi_param SCRIPT_NAME /dev3/<app_name>;
uwsgi_pass unix://var/run/uwsgi/<app_name>.sock;
When I access to the generated download like sent from my testing environment, it gives me an access to :
https://subdomain.domain.fr/download_export/<my_file> and not https://subdomain.domain.fr/dev3/<app_name>/download_export/<my_file>
It misses something ? How I can add this part /dev3/<app_name> ? By default, all other urls access to https://subdomain.domain.fr/dev3/<app_name>/something but not my generated link.
Thank you
EDIT :
In my uwsgi.ini file, I have this both lines :
mount = /dev3/%n=main.wsgi:application
manage-script-name=true
So it should work
You should check media_root in settings.py on your staging environment. Here's a great article on serving media files in Django that might help.
I have a Django website that uses Nginx on a separate file server to serve up images uploaded by my users. When any user views another user's photos, the link to the photo will have this structure:
https://fs01.mysite.com/media/photos/<username>_<photo-id>.jpg
When the page is rendered, the user's photo is served up by the file server (fs). But what is the Nginx way of handling incomplete (one could also say incorrect) requests to subdirectories of the above URL? For example, if a user enters this URL:
https://fs01.mysite.com
...they see Nginx's default "Welcome to nginx on ..." page. If they enter this URL:
https://fs01.mysite.com/media
...they'll get the Nginx "404 Not Found" page. And if they enter this URL:
https://fs01.mysite.com/media/photos
...they'll get the Nginx "403 Forbidden" page.
How do you handle these situations in Nginx?
They see welcome page, because the nginx matches default vhost entry instead of yours. You should probably delete the default one (/etc/nginx/conf.d/default or /etc/nginx/sites-enabled/default, depending on the linux distribution you use) and make sure that server_name line matches your url (i.e. no slash at the end).
Looks like your location block matckes only media/ (with the slash at the end). Thats why you get 404 error - it matches different location block and is probably handled by Django.
You are not allowed to list the directory, so you get the Forbidden error.
I think that it is up to you how such cases are handled, just make them consistent. Make sure that you understand nginx configuration and the pathways different requests take. To avoid 403 error, you can put empty index.html file in that directory.
I've configured a rails app on Digital Ocean (DO) using passenger, nginx and capistrano. Everything is working fine expect that I want to get the application's root_path page to be displayed whenever I go to the website. But that's not the case. I've to keep another page index.html in public directory and set it to root like:
xyz.conf file
root /home/user/apps/xyz/current/public;
I don't want this extra static page to be displayed. It just doesn't look good. Instead I want the application's root page which I specified in routes.rb file like root 'controller#method' to be displayed.
I've tried removing root line or restricting it to just /home/user/apps/xyz/current in .conf file but that just gives a 403 Forbidden error which should be the case.
I've thought a lot but can't think of anything. Anyone out there who had a similar problem and managed to figure it out?
Your nginx root is must be like:
root /home/rails/sample_tasks/public;
Note: path may vary but you need to set path till your app's public folder
then reload you Nginx
service nginx reload
I currently have an hosting website that previously hosted images locally.
Before the current system, image links were referenced from the root folder
EG:
https://example.com/image.jpg
We have since moved to AWS S3 storage and in order to direct legacy image requests to S3, we added the following to our nginx conf file.
location ~* ^/.*\.(jpg|gif|png|jpeg) {
rewrite ^/(.*)$ "$scheme://i.example.com/$1" permanent;
}
However this captures all image files on the domain.
Is there a way to only redirect requests for image requests on the root domain and ignore other folders?
For example, users avatars are stored locally in
https://example.com/content/images/user/qwerty.jpg
These get redirected to the subdomain due to the regex.
I also attempted this with the S3 proxy_pass. However results were the same.
Thanks for your help
To select only image files in the top level directory, you should replace the .* element of your regex with something that does not match /. Such as:
location ~* ^/[^/]*\.(jpg|gif|png|jpeg)$ {
return 301 $scheme://i.example.com/$request_uri;
}
An ember cli site was deployed onto a server and it works fine. Links via {{link-to}} all work beautifully.
BUT, when a user (me that is) manually enters a url and hits return. then the site is not found.
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
even changing a parameter of a working url (initially navigated to thru link-to)
http://site/start/0/length/30
and simply backspacing, changing the 30 to 20 and hit return
http://site/start/0/length/20
its a no go
localhost:4200 doesn't have this issue.
has anyone observed this vicious behaviour.
i actually need it for a callback redirect for oauth. but then noticed than any manually entered urls dont function.
It is because your server (IIS?) is trying to access the full path requested by your browser (eg /start/0/length/30), and not finding a valid file on disk returns a 404.
So, you need to configure your web server to proxy/rewrite the requests to the proper location. Assuming you are deploying your application in your "root" directory, the proper location is /index.html (the file ember-cli creates).
Unfortunately, I can't help you with IIS, but I can provide you with the proper configuration for nginx:
location / {
try_files $uri $uri/ /index.html;
}
This says "If the requested URI doesn't exist, instead respond with the /index.html file".
When you are using ember server on localhost:4200 you don't have the same problem because it is automatically doing something similar transparently.
If you are serving this up from any web server that isn't the built in Ember, ie non local server, you need to have a wildcard rule that returns your Ember app's index.html file for anything below your websites base url. If you only have your base url return the index.html file, then your webserver is confused by the unrecognized url and thinks it has nothing to return. If your rule, though,
for baseUrl/* returns index.html, your Ember app will then run the correct route hooks to establish the app context
this is a dupe and the question is
How to run emberJS application in IIS?
the easy answer is set locationType: hash in ember-cli's environment.config file (copied from accepted answer)
that will introduce a '#' in the url but doesnt require an IIS change.
var ENV = {
...
locationType: 'hash'
... };