I have a virtual host like below for my Yii website
server {
root /var/www/code;
index index.php index.html;
server_name mydomain.com;
location / {
rewrite ^/en/keyword$ /en/spesfic_controller/ last;
....
}
}
if i use last or break in the above rewrite rule, nginx throws 404 error, but if i change it to permanent, it will redirect it and will work fine!! what might be wrong? or what i missed?
(nothing in error log)
Related
I've a nuxt.js app behind nginx reverse proxy. The nginx conf file looks like this:
server {
listen 80;
# Match *.lvh.me
server_name ~^(?<user>.+)\.lvh\.me$;
location / {
proxy_pass http://localhost:8080/sites/$user$uri$is_args$args;
}
location ~* \.(?:js|css|jpg|jpeg|gif|png|ico|cur|svg)$ {
proxy_pass http://localhost:8080;
}
}
As you can see I'm mapping all my site subdomains to a specific path on my site and it is working fine. I'm also mapping all assets to be loaded from the root (because otherwise it throws a 404 error).
The only issue I'm facing is the whenever I visit a subdomain e.g subdomain.lvh.me it loads two pages on top of each other, the original page from subdomain root (which is expected) but also the page from the main domain root i.e. lvh.me (which is not expected).
Can you please checkout my conf file to see if I'm doing anything wrong here?
So I've encountered this issue and what I did to fix it was to not rely on Nginx's root nor proxy_pass. Instead, I used a location block with an alias and a try_files inside like so:
location ^~ / {
alias /path/to/dist;
try_files $uri $uri/ /index.html = 404;
}
I am trying to force SSL for a single subdirectory on my server by placing a rewrite rule in my nginx config file.
So, for example, when a user goes to example.com/billing or example.com/billing/user they are taken to https://example.com/billing or https://example.com/billing/user.
I have an SSL certificate installed etc. Here is a rule in my server block for nginx:
#billing location
location /billing/ {
if (!-e $request_filename){
rewrite ^/billing/(.*)$ /billing/index.php?request=$1 last;
}
}
Is there a way I can modify this rule to include forcing https?
I hope you have two server blocks one for http and other of http SSL connection; in your http server block adding redirect to https inside /billing/ location block will solve the issue.
server {
listen 80;
location /billing/ {
# 301 for permanent redirect
return 301 https://$host$request_uri;
}
}
I want to force HTTPS for certain subdomains in nginx. I also do not want to edit the .conf file when i create a new subdomain that needs HTTPS. Is it possible to force HTTPS if an SSL folder is present in the root or something equivalent to it? So all i have to do to enable HTTPS on a new subdomain is add a new folder or something like that?
Here is my nginx.conf file:
#HTTP DOMAIN.COM
server {
listen 80;
server_name *.domain.com;
root /var/www/$http_host;
location /ssl {
rewrite ^ https://$http_host/$1 redirect;
}
edit:
I've been messing around a bit with symlinks but no luck yet. This is the new configuration I made:
server {
listen 80;
server_name *.domain.com;
root /var/www/$http_host;
location /var/www/ssl/$http_host {
rewrite https://$http_host/$1 redirect;
}
include error_page;
include location_php;
}
Am I coming close to my answer?
edit:
What I basically want is to be able to use ssl without having to change my .conf file.
All I need to do to enable HTTPS is change something somewhere around the docroot.
I don't think you can enable SSL based merely on the existence of a directory. But you can extend the configuration using file globbing:
include vhosts/*.conf
include vhosts/ssl/*.conf
Just drop a minimal file in the directory.
(note that you still need to set up certificates, most likely on a per vhost basis - and these should not be incide the document root).
I got it working! this is the config file:
server {
listen 80;
server_name *.domain.com;
root /var/www/$host;
if (-e /var/www/config/ssl/$host) {
rewrite ^ https://$host$request_uri? permanent;
}
include error_pages;
include php_config;
}
I still had to use an if statement but that's alright because it works now.
I thought rewrite in Nginx would be straight-forward. It looks like its not.
I would like some help from you guys. My previous setup was Apache2 + PHP. My new setup is Nginx + PHP-FPM.
I would like to transform a simple virtualhost from Apache2.
The directory structure is:
/api (this is root)
VERSION (1)
--- api.php
--- .htaccess
Fx. domain.tld/1/
The .htaccess files looks like this:
RewriteEngine on
RewriteRule ^([a-z]+)/([a-z]+)/api.json$ api.php?app=$1&sapp=$2 [L,QSA]
RewriteRule ^([a-z]+)/api.json$ api.php?app=$1 [L,QSA]
My new Nginx server file looks like this:
server {
listen 80;
root /var/www/domain.tld/api;
index index.php index.html;
server_name domain.tld;
location /1/ {
rewrite ^/([a-z]+)/([a-z]+)/api\.json$ /api.php?app=$1&sapp=$2;
rewrite ^/([a-z]+)/api\.json$ /api.php?app=$1;
}
location ~ \.php$ {
include php5;
}
}
But it does not works when I try to view: domain.tld/1/view/api.json or domain.tld/1/view/extended/api.json
Can anyone explain and give some examples to fix this?
Thanks in advance.
Take a look at how NGiNX processes location blocks, here: http://nginx.org/en/docs/http/request_processing.html
As you can see in your case the request is only served by your location /1/ block, as that is the only one of the two that matches. Problem is, NGiNX only uses one location block per request, so once it got into the location /1/ block, it will not go to the other location block later.
The solution here is to also include your php5 file in the location /1/ block at the end.
location /1/ {
rewrite ^/([a-z]+)/([a-z]+)/api\.json$ /api.php?app=$1&sapp=$2;
rewrite ^/([a-z]+)/api\.json$ /api.php?app=$1;
include php5;
}
I'm using YII framework. I can access my site through: localhost/index.php
then If I click any links on it it says: 404 not found.
It works on Apache. I'm trying to configure it with NGINX with no success. Can somebody please tell me what can be the problem if something works with Apache but does not work with NGINX?
Log error from nginx:
2011/05/07 11:27:42 [error] 5104#3152: *30 CreateFile() "c:\EWemp\nginx-0.8.52/html/rooms/finished" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: localhost, request: "GET /rooms/finished HTTP/1.1", host: "localhost", referrer: "http://localhost/index.php"
So, I assume that it needs some kind of URL rewrite, since I do not have html/rooms/finished directory.
It is like html/controller/action/ but I do not know what to change in order to get it to work
Yii uses one index.php file to handle all client requests. You need to rewrite /rooms/finished to index.php/rooms/finished.
I have used this Nginx configuration to rewrite all requests to be handled by one index.php file. This configuration uses Fast-CGI to pass PHP requests to PHP-FPM. If you use proxy_pass, you can use rewrite. proxy_pass is explained here.
location / {
index index.php; # Set the index file
try_files $uri $uri/ #handler; # If missing pass the URI to front handler
}
location #handler {
rewrite / /index.php;
}
location ~ .php$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME PATH_TO_SCRIPT$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
In my opinion, may be you should make ".htaccess file" like in Apache.