nginx location can't have both rewrite and proxy - nginx

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/;
}

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 doesnt work on my CloudPanel Website

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

Nginx `location /` does not match `/some_path`

Help me pls to configure nginx: I want nginx return index.html for all URLs like 10.10.256.142/, 10.10.256.142/some_path and 10.10.256.142/other_path/lala. Problem: currently it returns index.htmlonly for 10.10.256.142/ URL.
My current settings
listen 80;
server_name 10.10.256.142;
server_name_in_redirect off;
resolver 127.0.0.1;
location / {
error_page 405 =200 $uri;
root /some_path/project_dir;
index index.html index.htm;
}
location /websocket {
# ....
For me, simplest solution would be:
root /some_path/project_dir;
location / {
rewrite ^ /index.html break;
}
location /websocket/ {
# ...
}
Just to complete the answer above and return static assets I had to write
root /srv/www/betbull;
location / {
if ($uri !~ (/assets/.*)) { # do not return index.html instead of static assets
rewrite ^ /index.html break;
}
}
UPDATE:
Better solution:
location / {
try_files $uri $uri/ index.html$query_string
}

nginx location directive not working as expected

The following configuration will rewrite /admin while proxy_pass /core, I can't figure out the reason. Any hint on this? Similar case like this Nginx: location regex for multiple paths with backend .
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /\#$1 break;
}
}
location ~ ^/(admin|core)/ {
proxy_pass http://127.0.0.1:8080/$1;
}
Using try_files will simplify things.
location /
try_files $uri $uri/ =404;
}
location ~ ^/(?:admin|core)/ {
proxy_pass http://127.0.0.1:8080;
}
Try this variant:
location ~ ^/(admin|core)(.*)$ {
proxy_pass http://127.0.0.1:8080/$1;
}

How can I rewrite this nginx command with try_file in Nginx?

I want to hide jsp extension in url by Nginx. How can I rewrite this nginx command with try_file in Nginx?
location ~ .*\.jsp$ {
root /var/www/html/www.domain.com;
if (!-f $request_filename) {
rewrite ^/(.*)$ /index.jsp?q=$1;
break;
}
location ~ \.jsp {
try_files $uri /index.jsp?q=$request_uri;
}
http://wiki.nginx.org/IfIsEvil

Resources