Nginx doesnt work on my CloudPanel Website - nginx

Open your server's root nginx.conf file, most of the time It's located it in: /etc/nginx/nginx.conf
Open the home directory of the script, you should be able to find this file (nginx.conf):
enter image description here
I am trying to put the nginx.conf into my Vhost config
server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
{{ssl_certificate_key}}
{{ssl_certificate}}
server_name ian.ph;
{{root}}
{{nginx_access_log}}
{{nginx_error_log}}
if ($bad_bot = 1) {
return 403;
}
if ($scheme != "https") {
rewrite ^ https://$host$uri permanent;
}
location ~ /.well-known {
auth_basic off;
allow all;
}
{{basic_auth}}
try_files $uri $uri/ /index.php?$args;
index index.php index.html;
location ~ \.php$ {
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
try_files $uri =404;
fastcgi_read_timeout 3600;
fastcgi_send_timeout 3600;
fastcgi_param HTTPS $fastcgi_https;
{{php_fpm_listener}}
{{php_settings}}
}
location / {
if (!-e $request_filename){
rewrite ^/$ /index.php?link1=home;
}
if (!-e $request_filename){
rewrite ^/aj/([^/.]+)/?$ /ajax.php?type=$1&first=$2;
}
rewrite ^/#([^\/]+)(\/|)$ /index.php?link1=timeline&id=$1;
if (!-e $request_filename){
rewrite ^/([^\/]+)(\/|)$ /index.php?link1=$1;
}
}
location /reset {
rewrite ^/reset-password/([^\/]+)(\/|)$ /index.php?link1=reset-password&code=$1;
}
location /confirm {
rewrite ^/confirm/(.*)/(.*)$ /index.php?link1=confirm&code=$1&email=$2;
}
location = /two_factor_login {
rewrite ^(.*)$ /index.php?link1=two_factor_login;
}
location = /two_factor_submit {
rewrite ^(.*)$ /index.php?link1=two_factor_submit;
}
location /v {
rewrite ^/v/(.*)$ /index.php?v=$1;
}
location /api {
rewrite ^/api/v(([0-9])([.][0-9]+))(\/|)$ /api.php?v=$1;
}
location /admin {
rewrite ^/admin-cp$ /admincp.php;
rewrite ^/admin-cp/(.*)$ /admincp.php?page=$1;
}
location /admin-cdn/ {
alias /admin-panel/;
}
location /videos {
rewrite ^/videos/category/(.*)/rss(\/|)$ /index.php?link1=videos&page=category&id=$1&feed=rss;
rewrite ^/videos/category/(.*)/(.*)$ /index.php?link1=videos&page=category&id=$1&sub_id=$2;
rewrite ^/videos/category/(.*)$ /index.php?link1=videos&page=category&id=$1;
rewrite ^/videos/(.*)/rss(\/|)$ /index.php?link1=videos&page=$1&feed=rss;
rewrite ^/videos/(.*)$ /index.php?link1=videos&page=$1;
}
location /articles {
rewrite ^/articles(\/|)$ /index.php?link1=articles;
rewrite ^/articles/category/(.*)(\/|)$ /index.php?link1=articles&category_id=$1;
rewrite ^/articles/read/(.*)(\/|)$ /index.php?link1=read&id=$1;
}
location /aj {
rewrite ^/aj/([^/.]+)/([^/.]+)/?$ /ajax.php?type=$1&first=$2;
rewrite ^/aj/([^/.]+)/([^/.]+)/([^/.]+)/?$ /ajax.php?type=$1&first=$2&second=$3;
}
location /edit {
rewrite ^/edit-video/(.*)?$ /index.php?link1=edit-video&id=$1;
}
location /video_text {
rewrite ^/video_text/(.*)?$ /index.php?link1=video_text&id=$1;
}
location /watch {
rewrite ^/watch/([^\/]+)(\/|)?$ /index.php?link1=watch&id=$1;
rewrite ^/watch/([^\/]+)/list/([^\/]+)(\/|)?$ /index.php?link1=watch&id=$1&list=$2;
}
location /embed {
rewrite ^/embed/(.*)?$ /index.php?link1=embed&id=$1;
}
location /resend {
rewrite ^/resend/(.*)/(.*)?$ /index.php?link1=resend&id=$1&u_id=$2;
}
location /redirect {
rewrite ^/redirect/(.*)?$ /index.php?link1=redirect&id=$1;
}
location /settings {
rewrite ^/settings/(.*)/(.*)$ /index.php?link1=settings&page=$1&user=$2;
rewrite ^/settings/(.*)$ /index.php?link1=settings&page=$1;
}
location /terms {
rewrite ^/terms/([^\/]+)(\/|)$ /index.php?link1=terms&type=$1;
}
location /go_pro {
rewrite ^/go_pro(\/|)$ /index.php?link1=go_pro;
}
location /ads {
rewrite ^/ads(\/|)$ /index.php?link1=ads;
rewrite ^/ads/create(\/|)$ /index.php?link1=create_ads;
rewrite ^/ads/edit/(\d+)(\/|)$ /index.php?link1=edit_ads&id=$1;
rewrite ^/ads/analytics/(\d+)(\/|)$ /index.php?link1=ads_analytics&id=$1;
}
location /contact {
rewrite ^/contact-us(\/|)$ /index.php?link1=contact;
}
location /messages {
rewrite ^/messages/(.*)$ /index.php?link1=messages&id=$1;
}
location /view_analytics {
rewrite ^/view_analytics/(.*)$ /index.php?link1=view_analytics&id=$1;
}
location /video_studio {
rewrite ^/video_studio/(.*)$ /index.php?link1=video_studio;
}
location = /comments {
rewrite ^(.*)$ /index.php?link1=comments;
}
location = /dashboard {
rewrite ^(.*)$ /index.php?link1=dashboard;
}
location = /popular_channels {
rewrite ^(.*)$ /index.php?link1=popular_channels;
}
location = /create_article {
rewrite ^(.*)$ /index.php?link1=create_article;
}
location = /my_articles {
rewrite ^(.*)$ /index.php?link1=my_articles;
}
location /edit_articles {
rewrite ^/edit_articles/(.*)?$ /index.php?link1=edit_articles&id=$1;
}
location = /age_block {
rewrite ^(.*)$ /index.php?link1=age_block;
}
location /site {
rewrite ^/site-pages/(.*)$ /index.php?link1=site-pages&page_name=$1;
}
location /post {
rewrite ^/post/(.*)(\/|)$ /index.php?link1=post&id=$1;
}
location /edit_activity {
rewrite ^/edit_activity/(.*)(\/|)$ /index.php?link1=edit_activity&id=$1;
}
location ~* ^.+\.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|eot|mp4|ogg|ogv|webm|webp|zip|swf)$ {
add_header Access-Control-Allow-Origin "*";
expires max;
access_log off;
}
if (-f $request_filename) {
break;
}
}
But stil doesnt work
Can someone please help me how i can put my nginx.conf into server
Heres my nginx.conf https://pastebin.com/raw/cx2h7gRu
Thanks

Related

Nginx URL wise redirect

I have a some case to map in nginx
for / it take from the /var/www/html/cont
for /content/* it take form the var/www/html/cont
if it is not / and not /content/*
the it should take from /var/www/html/web
i am stucking in case 3
Here is my config
location / {
root /var/www/html/cont;
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
# try_files $uri $uri/ =404;
}
location /content {
root /var/www/html/cont;
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
# try_files $uri $uri/ =404;
}
Any help appreciated.
Thank you
Set root for server {} context as root /var/www/html/web;
then
location / {
root /var/www/html/cont;
# your other directives
}
location /content {
root /var/www/html/cont;
# your other directives
}
Here is my solution which works perfectly in as per my use case.
location / {
if ($request_uri = "/") {
set $custom_root /var/www/html/cont;
}
if ($request_uri != "/") {
set $custom_root /var/www/html/web;
}
root $custom_root;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html break;
}
# try_files $uri $uri/ =404;
}
location /content {
root /var/www/html/cont;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html break;
}
}

nginx location can't have both rewrite and proxy

location = /land {
proxy_pass http://172.0.0.78:3033;
if ($http_user_agent ~* "(Android|iPhone|iPod|Symbian|BlackBerry|Windows Phone|Mobile|J2ME)") {
root /home/xxx/;
rewrite /(.*) /index.html;
break;
}
}
Excuse me, where is the above problem?
How should I modify it?
thks~
It's always wise to separate proxied and static content:
location = /land {
if ($http_user_agent ~* "(Android|iPhone|iPod|Symbian|BlackBerry|Windows Phone|Mobile|J2ME)") {
rewrite ^ /index.html last;
}
proxy_pass http://172.0.0.78:3033;
}
location = /index.html {
root /home/xxx/;
}

Jump #name gives me 404 error on nginx

When i click on a button using #jumpto it gives me a 404 error. I think it has something to do with the nginx config file because i am working on it.
How can I use #jump without giving me 404 error?
server {
listen 80;
server_name doutor.pt;
return 301 http://www.doutor.pt$request_uri;
}
server {
server_name doutor.pt www.doutor.pt;
access_log /var/log/nginx/doutor.pt.access.log;
error_log /var/log/nginx/doutor.pt.error.log;
root /var/www/doutor.pt/htdocs;
index index.php index.html;
location / {
try_files $uri $uri/ #rewrite;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
location #rewrite {
rewrite ^/sitemap-doctors-([0-9]+).xml$ /index.php?page_name=sitemap&category=doctors&page=$1 las$
rewrite ^/sitemap-doctors.xml$ /index.php?page_name=sitemap&category=doctors&page=0 last;
rewrite ^/sitemap.xml$ /index.php?page_name=sitemap last;
rewrite ^/category/([0-9]+)-([^/]+)$ /index.php?page_name=doctors&category=$1 last;
rewrite ^/medico/([^/]+)$ /index.php?page_name=medico&doctor_url=$1 last;
rewrite ^/([^/]+)/([^/]+)$ /index.php?page_name=$2&page_category=$2 last;
rewrite ^/([^/]+)$ /index.php?page_name=$1 last;
# some default action???
return 404;
}

Access index.html in folder without 301 redirect

I have some index.html files sitting in a folder to get some nice urls -
site.com/about
where index.html sits in the about folder. But I am seeing that my site.com/about is being 301 redirected to site.com/about/ I am not sure where the 301 is generated from. It is not in config.
/about/ also has a 301 result.
I guess it makes sense since I am redirecting to the index.html file but should it not be a rewrite? Is there a way to return 200 for /about instead of 301 to about/?
I am using nginx
Server Block:
server {
listen IP;
server_name site.com;
rewrite / $scheme://www.$host$request_uri permanent;
}
server {
listen IP:80;
server_name site.com *.site.com;
root /var/www/vhosts/site.com/htdocs;
charset utf-8;
rewrite_log on;
location / {
index index.html index.php;
try_files $uri $uri/ /$uri.php;
expires 30d;
}
if ($request_uri = /index.php) {
return 301 $scheme://$host;
}
if ($request_uri = /index) {
return 301 $scheme://$host;
}
location /. {
return 404;
}
location ~ .php/ {
rewrite ^(.*.php)/ $1 last;
}
include "ssl_offloading.inc";
location ~ .php$ {
# if (!-e $request_filename) { rewrite / /index.php last; }
if (!-e $request_filename) { rewrite / /404.php last; }
}
}
The index directive and the $uri/ element of the try_files directive, has the side-effect of adding a trailing / to directory names by performing an external redirect.
To avoid the external redirect and return an appropriate index file when presented with a slash-less directory name, implement the index functionality explicitly within the try_files directive:
location / {
try_files $uri $uri/index.html $uri.php;
expires 30d;
}
Notice that .php works only in the last element at this location. If you need to check for $uri/index.php (in addition to $uri.php) you can use a named location block - and move or copy your fastcgi configuration into it.
For example (based on your server block):
root /var/www/vhosts/site.com/htdocs;
error_page 404 /404.php;
location / {
try_files $uri $uri/index.html #php;
expires 30d;
}
location #php {
try_files $uri.php $uri/index.php =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
...
fastcgi_pass ...;
}
location = /index.php { return 301 $scheme://$host; }
location = /index { return 301 $scheme://$host; }
location /. { return 404; }
location ~* \.php(/|$) { rewrite ^(.*)\.php $1 last; }
include "ssl_offloading.inc";

Nginx auth_basic and rewrite in subdirectory does not work

I'm trying to add a project to a subfolder of existing webserver with Nginx. Here's my simple config:
server {
listen 80 default_server;
server_name localhost;
root /var/www;
[...]
location = /my-project { return 301 /my-project/; }
location /my-project/ {
alias /var/www/my-project/web/;
index index.php;
location ~ /[^/]+/control(/|$) {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd;
if (-f $request_filename) { break; }
rewrite ^(.*)$ /my-project/index.php last;
}
if (-f $request_filename) { break; }
rewrite ^ /my-project/index.php last;
location ~ ^/[^/]+/index\.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/fcgi.sock;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
}
}
Because there is an rewrite directive inside /control location block, the auth_basic never gets triggered, because Nginx executes rewrites before authentication. In which way should I modify the config, that auth does work?
PS: try_files doesn't seem to work, because it serves files from root (/) webfolder!? When I replace the if and following rewrite with try_files $uri /my-project/index.php?$query_string; I get a 404, because Nginx tries to serve the file /var/wwwindex.php (have a look at missing slash and the root folder /var/www instead of alias).
EDIT 18.09.2013:
As VBart suggests I'm using now the following configuration to get authentication to work
location ~ ^/(?<dir>my-project)(?<path>/.*)$ {
root /var/www/$dir/web;
location ~ ^/[^/]+/control(/|$) {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd;
try_files $path /$dir/index.php?$query_string;
}
try_files $path /$dir/index.php?$query_string;
location ~ ^/[^/]+/index\.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/fcgi.sock;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
}
Use:
error_page 404 =200 /my-project/index.php;
instead of ugly rewrites:
if (-f $request_filename) { break; }
rewrite ^(.*)$ /my-project/index.php last;
Reference:
http://nginx.org/r/error_page
http://wiki.nginx.org/IfIsEvil
http://wiki.nginx.org/Pitfalls
P.S. try_files doesn't work with alias because of bug: http://trac.nginx.org/nginx/ticket/97, but you can replace alias with root directive: http://nginx.org/r/root

Resources