I have my-domain.com and prod.my-domain.com and I have created the following configuration settings for both.
prod.my-domain.com
server {
listen 80;
server_name prod.my-domain.com;
location / {
root /usr/share/nginx/html/;
index index.php index.html index.htm;
}
location /prod{
autoindex on;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/index/html/prod/;
}
error_page 500 502 503 504 /50x.html;
location = /50.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_intercept_errors on;
error_page 404 /path/to/404.htm;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
my-domain.com
server{
listen 80;
server_name my-domain.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_intercept_errors on;
error_page 404 /path/to/404.htm;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
But, whichever URL I enter takes me where all the files for my-domain.com are stored.
I would like to see the contents for root /usr/share/nginx/index/html/prod/ when I visit
prod.my-domain.com but, it takes me to root /usr/share/nginx/index/html/
Related
This is my current setup for my /etc/nginx/conf.d/default.conf file on my nginx web server. I have a Wordpress installation and I am attempting to use the pretty permalinks structure like so: http://XXX.XXX.XX.X/kiosks/%postname%/
Currently getting an "No input file specified." error. Below is my default.conf:
#
# The default server
#
server {
listen 80;
server_name example.com;
root /usr/share/nginx/html;
# location / {
# root /usr/share/nginx/html;
# index index.php index.html index.htm;
# }
location /kiosks {
index index.php index.html index.htm;
try_files $uri $uri/index.php?$args;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
Solution:
#
# The default server
#
server {
listen 80;
server_name example.com;
root /usr/share/nginx/html;
# location / {
# root /usr/share/nginx/html;
# index index.php index.html index.htm;
# }
location #wp {
rewrite ^/kiosks(.*) /kiosks/index.php?$1;
}
location /kiosks {
alias "/usr/share/nginx/html/kiosks/"; #where wp actually is
try_files $uri $uri/ #wp;
location ~ \.php$ {
try_files $uri =404;
fastcgi_read_timeout 300s;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
# location /kiosks {
# try_files $uri $uri/index.php?$args;
# index index.php index.html index.htm;
# }
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
nginx multiple websites on same port and one ip address
for example my ip 192.167.10.2
and we want to run site on same ip
nginx/html contain two project like below
1: project1
2: project2
we run default project1 on ip :192.167.10.2
and second project run like 192.167.10.2/project2
how do configuration in nginx config file
Here is a simple example.You could try
First config :
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/mywebsite1;
index index.php index.html index.htm;
server_name mywebsite1.de www.mywebsite1.de;
location / {
try_files $uri/ /index.php?$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/mywebsite1;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Second config:
server {
listen 80;
listen [::]:80 //#here Be carefull , only one default
root /var/www/mywebsite2;
index index.php index.html index.htm;
server_name mywebsite1.de www.mywebsite1.de;
location / {
try_files $uri/ /index.php?$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/mywebsite1;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Don't forget to restart server.
There are 2 second-level domain names and 2 corresponding folders,like this:
domain name folder
111.aa.com /var/www/111.aa.com
222.aa.com /var/www/222.aa.com
In nginx.conf,there are 2 server blocks,like this:
#111.aa.com
server {
listen 80;
server_name 111.aa.com;
charset utf-8;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /var/www/111.aa.com;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
location ~ \.php$ {
root /var/www/111.aa.com;
fastcgi_pass unix:/dev/shm/php-fpm.sock;
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
#222.aa.com
server {
listen 80;
server_name 222.aa.com;
charset utf-8;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /var/www/222.aa.com;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
location ~ \.php$ {
root /var/www/222.aa.com;
fastcgi_pass unix:/dev/shm/php-fpm.sock;
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Questions:
1、Do two server blocks have to be required?If there are 100 second-level domain names,I have to write 100 server blocks,is it ok to write in one server block?How to write in one server block?Is there an example?
2、Writing in one server block and writing in respective 100 server blocks,which one is better?
I'm trying to setup phpmyadmin on my domain, and for some reason, I can't have the server root I want.
This doesn't work (404 on example.com/phpmyadmin without anything in the logs):
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
client_max_body_size 300m;
root /var/www/html;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri $uri/ =404;
}
location /phpmyadmin/ {
root /var/www/admin/;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
But if I change the server root to /usr/share/nginx/html it works...
Do you have any idea of what is happening?
Thank you for reading me.
Ok I fixed it, thanks to this post : nginx configuration with multiple location blocks
The reason was the ~ location for php files...
So here is the working code :
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
client_max_body_size 300m;
root /var/www/html;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri $uri/ =404;
}
location /phpmyadmin/ {
root /var/www/admin/;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
I have a problem with both RoundCube and phpMyAdmin. I have varnish running on port 80 and nginx running on 8080.
When I go to phpmyadmin.domain and log in, it will redirect me to phpmyadmin.domain:8080.
When I go to webmail.domain and try to log in, it keeps reloading the login page unless you go to webmail.domain:8080 and log in, then it will work.
I have tried
port_in_redirect off;
but it still seems to need 8080.
Nginx config for phpmyadmin:
server {
listen 8080;
server_name phpmyadmin.domain.name;
port_in_redirect off;
access_log /var/log/nginx/phpmyadmin.access.log;
error_log /var/log/nginx/phpmyadmin.error.log;
root /var/www/phpmyadmin.domain.name/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
Nginx config for webmail:
server {
listen 8080;
server_name webmail.domain.name;
root /usr/share/roundcubemail;
port_in_redirect off;
# Logs
access_log /var/log/roundcubemail/access.log;
error_log /var/log/roundcubemail/error.log;
# Default location settings
location / {
index index.php;
try_files $uri $uri/ /index.php?$args;
}
# Redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
#error_page 404 /404.html;
# Pass the PHP scripts to FastCGI server (locally with unix: param to avoid network overhead)
location ~ \.php$ {
# Prevent Zero-day exploit
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Deny access to .htaccess files, if Apache's document root
location ~ /\.ht {
deny all;
}
# Exclude favicon from the logs to avoid bloating when it's not available
location /favicon.ico {
log_not_found off;
access_log off;
}
}
As AlexeyTen mentioned above, php server port was the problem in /etc/nginx/fastcgi_params I changed:
fastcgi_param SERVER_PORT $server_port;
to:
#fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_PORT 80;
and it has resolved my problems!